Mei*_*iji -2 php string-comparison
假设我们有两个字符串,我想逐个字符地比较它们,以检查字符串a和字符串b的任何字符是否匹配?
一个例子:
$a = "Hello";
$b = "world";
Run Code Online (Sandbox Code Playgroud)
在上面的'o'中都存在两个字符串,所以算法应该回显exist.
如果拆分字符串并使用array_unique删除重复项,则array_intersect将为您提供两个字符串中的字符.
$a = "Hello";
$b = "world";
$matching = array_unique(array_intersect(str_split(strtolower($a)), str_split(strtolower($b))));
if(count($matching)>0) echo "matching characters: " . implode(", ", $matching);
//matching characters: l, o
Run Code Online (Sandbox Code Playgroud)
罗恩建议增加了strtolower.