Take accentuation of a string

2

I am getting a string by POST, and I want to take accent and add '_' if there is white space. I used strtr but it does not work for me.

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

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

echo $aa;
    
asked by anonymous 11.12.2014 / 12:16

4 answers

4
if (isset($_POST['txtnome'])) 
{
    $txtnome = htmlentities($_POST['txtnome']);
}
 //$txtnome = 'á é í oo â ã'; exemplo retorna a_e_i_oo_a_a
 $aa = 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 $aa;
    
11.12.2014 / 12:27
3

You need to decode before:

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

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

echo $aa;

See running on ideone .

    
11.12.2014 / 12:24
1

You can also 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

    
11.12.2014 / 12:26
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 accents will be "translated" to ' and ~ for example, then I created another method to change spaces and unnecessary characters by _ :

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 ç');
    
04.07.2015 / 15:46