-3

如果我想匹配两个字符串

$string1 = 'hello';
$string2 = 'hell';

或者

$string1 = 'hello';
$string2 = 'hellz';

所以结果应该是这样的“4个字符匹配”。

4

2 回答 2

2
$string1 = 'hello';
$string2 = 'hell';

$matchingCount = 0;
for($n = 0; $n < max(strlen($string1), strlen($string2)); $n++) {
    if(substr($string1, $n, 1) == substr($string2, $n, 1)) {
        $matchingCount++;
    }
}

echo $matchingCount.' Character are match';

jsFiddle

于 2012-12-04T07:54:10.557 回答
1
   <?php 
  $s1='hello';
        $s2='hellz';
        $s1_array=str_split($s1);
        $s2_array=str_split($s2);
        $intersect_result=array_intersect($s1_array,$s2_array);
        $matchCount=count($intersect_result);
       echo $matchingCount.' Characters are matched';
        ?>
于 2012-12-04T10:04:33.770 回答