Compare letters without accents with accented letters

0

I decided to make a mini game of the gallows just not to rust in php, I'm having problems with words with accents, eg apple. Initially I get it from an array and separate itself into an array by getting (m, a, ç, ã). In the game I have a virtual keyboard of the alphabet without accent, how do I when the user press the a key of the keyboard it besides showing the letter a of the second position also show the with accent? would be to compare one character with another by removing the accent from that other. I did not post code because in this case it is irrelevant, since it is only to make (a == ã) true.

    
asked by anonymous 05.02.2016 / 04:16

3 answers

3

There is no native function that does this. What I would say to do is to create a function that takes out the accents before comparing. example found here

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

then compare what user entered with the letter without accent

(tirarAcentos('a') == tirarAcentos('ã')) 
    
05.02.2016 / 04:36
3

You can use iconv to return the letter / word without an accent.

For example, using maçã :

<?php
    setlocale(LC_CTYPE, 'pt_BR');
    // Necessário para definir os acentos brasileiros

    $array = array('m','a','ç','ã');
    // Array base

    foreach($array as $letra){
    echo iconv('UTF-8', 'ascii//TRANSLIT', $letra);
    // Resultado: maca
    }
?>

If you'd like you can test this here . :)

To compare with the letter chosen by the user you can create something like:

<?php
    setlocale(LC_CTYPE, 'pt_BR');
    // Necessário para definir os acentos brasileiros

    $array = array('m','a','ç','ã');
    // Array base

    foreach($array as $letra){
        if(($_POST['letra'] == iconv('UTF-8', 'ascii//TRANSLIT', $letra)){
            // m == m, a == a, c == c, a == a
            echo "Letra certa";
        }
    }
?>
  

The charset (the first variable of iconv ) was changed from utf8 to UTF-8 for compatibility reasons. Some situations utf8 may display " Wrong charset " error, while UTF-8 corrects this. Remember that the correct ( link ) is UTF-8 . ;)

    
05.02.2016 / 15:48
0

As our friend user1811893 replied, there is no native function that does this. However, I will share the function I use. While its function removes all accents and spaces, the below also removes the accents, but changes the spaces by underlines ( _ ):

function removerAcentuacao($string){
    $string = preg_replace("/[áàâãä]/", "a", $string);
    $string = preg_replace("/[ÁÀÂÃÄ]/", "A", $string);
    $string = preg_replace("/[éèê]/", "e", $string);
    $string = preg_replace("/[ÉÈÊ]/", "E", $string);
    $string = preg_replace("/[íì]/", "i", $string);
    $string = preg_replace("/[ÍÌ]/", "I", $string);
    $string = preg_replace("/[óòôõö]/", "o", $string);
    $string = preg_replace("/[ÓÒÔÕÖ]/", "O", $string);
    $string = preg_replace("/[úùü]/", "u", $string);
    $string = preg_replace("/[ÚÙÜ]/", "U", $string);
    $string = preg_replace("/ç/", "c", $string);
    $string = preg_replace("/Ç/", "C", $string);
    $string = preg_replace("/[][><}{)(:;,!?*%~^'&#@]/", "", $string);
    $string = preg_replace("/ /", "_", $string);
    return $string;
}
    
05.02.2016 / 14:22