Take accent from a string

Asked

Viewed 1,914 times

3

I’m getting a string by POST, and I want to take out accent and add '_'if there is white space. I used the strtr but it doesn’t work for me.

if (isset($_POST['txtnome'])) 
{
    $txtnome= htmlentities($_POST['txtnome']);
}

$aa = strtr($txtnome,'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ','AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');

echo $aa;
  • These code cracks are typos.

  • typos, as well as?

  • Look at the edition I made. your code is like this on your machine?

  • 2

    Related: http://answall.com/questions/858/refatora%C3%A7%C3%A3o-de-fun%C3%A7%C3%A3o-para-remover-scor%C3%A7%C3%A3o-espa%C3%A7os-e-caracteres-especiais, http://answall.com/s/33029/like-remover-question-acento-no-upload-comphp.

  • Note: Actually the questions I indicated solve the same problem (remove accentuation), but only here we have answers that correct this specific code. So I chose to keep this open.

4 answers

4

You need to decode first:

if (isset($_POST['txtnome'])) 
{
    $txtnome= htmlentities($_POST['txtnome']);
}

$aa = strtr(utf8_decode($txtnome), utf8_decode(' àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), '_aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');

echo $aa;

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 4

    Could someone tell what’s wrong to give a -1?

  • I think someone didn’t like it, it’s quite common around here (although it’s not a valid reason).

4


if (isset($_POST['txtnome'])) 
{
    $txtnome = $_POST['txtnome']; // COM O htmlentities ELE CONVERTE ACENTOS EM CODIGOS HTML, sem ele o str_replace funcionará. TESTADO EM UM SCRIPT MEU. BY AKSG.
}
 //$txtnome = 'á é í oo â ã'; exemplo retorna a_e_i_oo_a_a
 $txtnome = str_replace( array(' ', 'à','á','â','ã','ä', 'ç', 'è','é','ê','ë', 'ì','í','î','ï', 'ñ', 'ò','ó','ô','õ','ö', 'ù','ú','û','ü', 'ý','ÿ', 'À','Á','Â','Ã','Ä', 'Ç', 'È','É','Ê','Ë', 'Ì','Í','Î','Ï', 'Ñ', 'Ò','Ó','Ô','Õ','Ö', 'Ù','Ú','Û','Ü', 'Ý'), array('_', 'a','a','a','a','a', 'c', 'e','e','e','e', 'i','i','i','i', 'n', 'o','o','o','o','o', 'u','u','u','u', 'y','y', 'A','A','A','A','A', 'C', 'E','E','E','E', 'I','I','I','I', 'N', 'O','O','O','O','O', 'U','U','U','U', 'Y'), $txtnome); 

echo $txtnome;
  • It may just be a typo when pasting the code into the question... and at no time does it replace spaces by _ (str_replace)

  • 1

    I did a test with this code and it doesn’t work: http://ideone.com/0L48Ht

  • It keeps printing with the seats, but the parts of the spaces work.

  • I changed the code @akm

  • The problem is that whenever you write the string in the coded page, for example $txtnome=Éé, the various examples work, but as I receive by POST, always print with the accent.

  • Did you accept my answer because it was closer to what you wanted or that it really worked for you? @akm

  • What happens is what I get(txtnome) is an input text on another page, and I think it’s not a string. The accent part does not change, but the space part works. If you put the variable in quotes, for example txtnome="Éé" works.

Show 2 more comments

1

It is also possible to do this with str_replace, the first argument uses an array with accented characters and the second with the characters to be replaced.

$acentos = array('À', 'Á','Â','Ã','Ä','Å','Ç','È','É','Ê','Ë','Ì',
'Í','Î','Ï','Ò','Ó','Ô','Õ','Ö','Ù','Ú','Û','Ü','Ý','à','á','â','ã','ä','å','ç','è'
,'é','ê','ë','ì','í','î','ï','ð','ò','ó','ô','õ','ö','ù','ú','û','ü','ý','ÿ', ' ');

$sem_acentos = array('A','A','A','A','A','A','C','E','E','E','E','I','I','I',
'I','O','O','O','O','O','U','U','U','U','Y','a','a','a','a','a','a','c','e','e','e'
,'e','i','i','i','i','o','o','o','o','o','o','u','u','u','u','y','y', '_');

$txtnome = 'AçÃO í è';

echo 'string original: '. $txtnome;

$txtnome = str_replace($acentos, $sem_acentos, $txtnome);

echo '<br> string: sem acentos'. $txtnome;

Example

0

An alternative may be to use iconv, in case you can use a method like this:

function translitAscii($texto)
{
    $encode = mb_detect_encoding($texto, mb_detect_order(), true);
    return 'ASCII' === $encode ? $texto: iconv($encode, 'ASCII//TRANSLIT//IGNORE', $text);
}

echo translitAscii('áéí asd asd ã è');//Saida: 'a'e'i asd asd ~a `e

In case the accents will be "translated" to ' and ~ for example, so I created another method to exchange unnecessary spaces and characters for _:

function customFormatString($texto)
{
    $texto = translitAscii($texto);

    //Remove todos diferentes de A-Za-z0-9_ (adicione mais caracteres permitidos aqui) incluindo o espaço
    $texto = preg_replace('/[^A-Za-z0-9_]/', '_', $texto);

    //Remove repetições do underline, por exemplo: a__b_c ficará a_b_c
    $texto = preg_replace('/[_]+[_]/', '_', $texto);

    //Remove _ do começo e do fim da string, por exemplo: _a_b___ ficará a_b
    return trim($texto, '_');
}

echo customFormatString('á é í ó u ç');

Browser other questions tagged

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