Compares two strings and ignores (but does not replace) accents. PHP
P粉476046165
P粉476046165 2023-08-17 16:16:51
0
2
473
<p>I get (for example) two strings: </p> <pre class="brush:php;toolbar:false;">$a = "joao"; $b = "joão"; if (strtoupper($a) == strtoupper($b)) { echo $b; }</pre> <p>I hope it is true even with the accent. However, I need it to ignore accents rather than replace, since I need to output "joão" instead of "joao". </p> <p>All the answers I've seen replace "ã" with "a" instead of making the comparison true. I've been reading about normalization but I can't get it to work either. Any ideas? Thanks. </p>
P粉476046165
P粉476046165

reply all(2)
P粉068510991

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粉470645222

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)

Demo

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!