我需要比较两个字符串,例如INTU
和IXTE
,并检查是否有两个或更多字符相同。对于上述两个字符串,我希望返回true
,因为I和T相同。
字符串中字母的顺序最终无关紧要,因为每个字符不能出现在字符串的不同位置。看起来应该有一种简单的方法来做到这一点。
我需要比较两个字符串,例如INTU
和IXTE
,并检查是否有两个或更多字符相同。对于上述两个字符串,我希望返回true
,因为I和T相同。
字符串中字母的顺序最终无关紧要,因为每个字符不能出现在字符串的不同位置。看起来应该有一种简单的方法来做到这一点。
看一下similar_text()
函数。下面的代码没有经过测试,但我认为它可以按照您的要求工作。
$a = "INTU";
$b = "IXTE";
$is_match = ( similar_text($a , $b) >= 2) ;
Manual: http://php.net/manual/de/function.array-intersect.php
strings
不是字符的array
。有没有办法将它们视为这样的数组以便与此函数一起使用? - rapidashcount(array_intersect(str_split($a), str_split($b))) >= 2)
,但现在我知道了上面提到的方法,所以会进行修改。 - rapidashfunction compare_strings($str1, $str2)
{
$count=0;
$compare[] = substr($str1, 0, 1);
$compare[] = substr($str1, 1, 1);
$compare[] = substr($str1, 2, 1);
$compare[] = substr($str1, 3, 1);
foreach($compare as $string)
{
if(strstr($str2, $string)) { $count++; }
}
if($count>1)
{
return TRUE;
}else{
return FALSE;
}
}
$is_match = ( similar_text($a , $b) >= 0.5);
可能是正确的,因为similar_text
似乎返回一个百分比。 - rapidash