如果我想匹配两个字符串,如
$string1 = 'hello';
$string2 = 'hell';
OR
$string1 = 'hello';
$string2 = 'hellz';
所以结果应该是这样的“4个字符匹配”。
答案 0 :(得分: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';
答案 1 :(得分: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';
?>