Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我想匹配两个字符串
$string1 = 'hello'; $string2 = 'hell';
或者
$string1 = 'hello'; $string2 = 'hellz';
所以结果应该是这样的“4个字符匹配”。
$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';
<?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'; ?>