P粉0685109912023-08-18 11:34:44
I'd like to share an elegant solution that avoids using htmlentities and doesn't require manually listing all character replacements. This is the php translation of this post.
function removeAccents($str) { return preg_replace('/[\x{0300}-\x{036f}]/u',"",normalizer_normalize($str,Normalizer::FORM_D)); } $a = "joaoaaeeA"; $b = "joãoâàéèÀ"; var_dump(removeAccents($a) === removeAccents($b));
Output:
bool(true)
P粉4706452222023-08-18 10:51:27
Just convert the accented characters to their unaccented counterparts and then compare the strings. The function in my answer will remove accents for you.
function removeAccents($string) { return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '', htmlentities($string, ENT_QUOTES, 'UTF-8'))), ' ')); } $a = "joaoaaeeA"; $b = "joãoâàéèÀ"; var_dump(removeAccents($a) === removeAccents($b));
Output:
bool(true)