Home  >  Q&A  >  body text

Compares two strings and ignores (but does not replace) accents. PHP

<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粉476046165P粉476046165451 days ago469

reply all(2)I'll reply

  • P粉068510991

    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)

    reply
    0
  • P粉470645222

    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)

    Demo

    reply
    0
  • Cancelreply