Compares two strings and ignores (but does not replace) accents. PHP
P粉476046165
2023-08-17 16:16:51
<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>
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.
Output:
Just convert the accented characters to their unaccented counterparts and then compare the strings. The function in my answer will remove accents for you.
Output:
Demo