检查字符串的子字符串是否是另一个字符串的子字符串

时间:2015-10-14 13:55:56

标签: php string substring

我有两个字符串

$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";

现在我必须检查$Str2中的$Str1的子字符串是否已退出。例如,在这种情况下 C D E

这是一个基本而简单的例子。我遇到了复杂的情况,两个字符串中的数据都是动态的。

如果我必须比较完整的字符串 strpos 是解决方案,但在这种情况下该怎么办?

3 个答案:

答案 0 :(得分:2)

此功能适用于任何字符串

function test($str1, $str2)
{
    for ($i = strlen($str1); $i != 0; $i--) {
        for ($x = 0; $x < strlen($str1) - $i; $x++) {
            if (($pos = strpos($str2, substr($str1, $x, $i + 1))) !== false) {
                return substr($str1, $x, $i + 1);
            }
        }
    }
    return null;
}

返回最大可能匹配或null。

$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
echo test($str1, $str2); // omnis iste natus error sit voluptatem

答案 1 :(得分:1)

此功能适用于您:

<?php
$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";

echo strCompare($Str1, $Str2); //Returns C D E

function strCompare($Str1, $Str2) {
    $str1 = explode(" ", $Str1);
    $str2 = explode(" ", $Str2);

    $match = false;
    $newStr = [];

    foreach($str1 as $key => $value){
        if($match == true){
            if(isset($str2[$key]) && $str2[$key] == $value){
                $newStr[] = $value;
            } else {
                break;
            }
        } else {
            if(isset($str2[$key]) && $str2[$key] == $value){
                $match = true;
                $newStr[] = $value;
            }   
        }
    };

    return implode(" ",$newStr);

}

答案 2 :(得分:0)

也许这个可能也有帮助:

<?php
/**
 * returns an array of positions and substrings of needle that were found 
 * in string haystack using a minimal and maximal substring length
 * 
 * @param unknown $haystack find substrings of needle in this string
 * @param unknown $needle substrings in needle will be searched in haystack
 * @param number $min minimal length of a substring
 * @param number $max maximum length of a substring
 * @return array empty if no result
 */
function intersect_string($haystack,$needle,$min=3,$max=25)
{
    $r = [];
    for($j=$i=0;isset($needle[$i])&&isset($needle[$i+1]);$j=0,$i++) {
        if (!($c=$needle[$i].$needle[$i+1])) continue;
        while (($p=strpos($haystack,$c,$j))!==false) {
            $r[$p] = $c;
            for ($j=$p+2,++$i;isset($needle[$i+1])&&isset($haystack[$j]);$j++,$i++) {
                if (!($needle[$i+1]==$haystack[$j])) break;
                $r[$p] .= $haystack[$j];
            }
            $l = strlen($r[$p]);
            if ($l<$min||$l>$max) { unset($r[$p]); }
        }
    }
    return $r;
}
header('Content-Type:text/plain');
$str1 = "A little wolf is jumping over the tiny cow";
$str2 = "A tiny cow is jumping over the little wolf";
var_dump(intersect_string($str1,$str2,2));
var_dump(intersect_string($str1,$str2,3,12));
$str1 = "A B C D E F G H I J";
$str2 = "1 2 C D E P Q R";
var_dump(intersect_string($str1,$str2,1));
var_dump(intersect_string($str1,$str2,8));
$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
var_dump(intersect_string($str1,$str2,3,40));
?>

这将导致:

array(4) {
  [0]=>
  string(2) "A "
  [34]=>
  string(8) "tiny cow"
  [13]=>
  string(21) " is jumping over the "
  [2]=>
  string(11) "little wolf"
}
array(2) {
  [34]=>
  string(8) "tiny cow"
  [2]=>
  string(11) "little wolf"
}
array(1) {
  [3]=>
  string(7) " C D E "
}
bool(false)
array(2) {
  [0]=>
  string(24) "Sed ut perspiciatis unde"
  [24]=>
  string(38) " omnis iste natus error sit voluptatem"
}