php function cannot compare words with accents

Asked

Viewed 93 times

-2

I have a function that compares 2 words, but when removing the accents, it still identifies as different words:

function acento($str, $from, $to) {
    $keys = array();
    $values = array();
    preg_match_all('/./u', $from, $keys);
    preg_match_all('/./u', $to, $values);
    $mapping = array_combine($keys[0], $values[0]);
    return strtr($str, $mapping);
}
$from = "áàãâéêíóôõúüçÁÀÃÂÉÊÍÓÔÕÚÜÇ";
$to = "aaaaeeiooouucAAAAEEIOOOUUC";

foreach($items as $i){

    $query=acento($_POST['item'], $from, $to);
    $n = mb_strlen($_POST['item'],'utf8');
    $q=acento($i, $from, $to);
    $q=substr($q,0,$n);


    if($query==$q) {
        echo 'teste'; echo "<div style='border-bottom: 1px solid #ccc;margin-bottom:3px'><button type='button' value='{$i}' class='getdemanda uk-button uk-button-text uk-button-large' style='font-size:20px'>{$i}</button></div>";
    }else{
        var_dump($q,$query);
        echo '<br/>';
    }

}

When I make a if($q==$query) it gives false result, even in var dump giving exactly the same thing to the two.

Example of var_dump($q==$query) with text "copy"

STRING(4) "COPI" STRING(4) "COPI"
  • What is the content of $_POST['item']?

  • 1

    What’s in the $items?

1 answer

0


The solution to this link may work Remove accents from a php string

I took the following test

function tirarAcentos($string){
    return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"),$string);
}

$sem = tirarAcentos("Olá mundo!");
$hello = "Ola mundo!";

var_dump($sem, $hello, $hello == $sem);

And the result was: string(10) "Ola mundo!" string(10) "Ola mundo!" bool(true)

  • worked out!!! , thanks

  • Dispo @Leandromarzullo. Guys had already given a negative kkkk people is bone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.