I do not see any answers that drew attention to the fact that capital letters have different characters than lowercase for count_chars ()
if (isAnagram('Polo','pool')) {
print "Is anagram";
} else {
print "This is not an anagram";
}
function isAnagram($string1, $string2)
{
if (strlen($string1) != strlen($string2)) {
return false;
}
$array1 = count_chars(strtolower($string1));
$array2 = count_chars(strtolower($string2));
if (!empty(array_diff_assoc($array2, $array1))) {
return false;
}
if (!empty(array_diff_assoc($array1, $array2))) {
return false;
}
return true;
}
source
share