Compare accented letters with accented letters

Asked

Viewed 1,284 times

0

I decided to make a mini hangman game just to not rust in php, I’m having problems with words with accents, example: apple. Initially, I take it from an array and separate it into an array, leaving (m, a, ç, ã). In the game I have a virtual keyboard of the alphabet without accent, how do I when the user presses on the 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 this other’s accent. I didn’t post code because in this case it’s irrelevant, since it just makes (a == ã) true.

  • How are you mounting this keyboard ? the ideal would be a keyboard with ABTN pattern until some words with accent sound different

  • I see no problem in it, the keyboard I have is as I quoted, only the alphabet, no accent, like all hangman games.

3 answers

3

You can use the iconv to return the letter/word without 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 want you can test this here. :)

So you can compare to the letter chosen by the user you can create something similar to:

<?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 for UTF-8 for reasons of compatibility. Some situations utf8 may present an error of "Wrong charset", as long as the UTF-8corrects it. It is worth remembering that the correct (http://www.ietf.org/rfc/rfc3629) is the UTF-8. ;)

  • Your link is broken, friend @Inkeliz

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('ã')) 
  • "There is no native function that does this". It depends on how you consider the iconv(). By the way, "Ç" is missing in its function.

0

As our friend user1811893 answered, 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 accents, but swaps spaces for 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;
}

Browser other questions tagged

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