1

Please, this is not duplicated, my problem is not about converting the letter.

i read some values from a database and some of them contains some weird accent, such as "Piña_Colada" "Rhum_Arrangé". i got no control on the DB and i want to delete the accents and replace it with normal letter -> "Pina_Colada" "Rhum_Arrange"

Here is a way i have find on stackoverflow

function remove_acc($str){
$caracteres = array(
'À' => 'a', 'Á' => 'a', 'Â' => 'a', 'Ä' => 'a', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ä' => 'a', '@' => 'a',
'È' => 'e', 'É' => 'e', 'Ê' => 'e', 'Ë' => 'e', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', '€' => 'e',
'Ì' => 'i', 'Í' => 'i', 'Î' => 'i', 'Ï' => 'i', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'Ò' => 'o', 'Ó' => 'o', 'Ô' => 'o', 'Ö' => 'o', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'ö' => 'o',
'Ù' => 'u', 'Ú' => 'u', 'Û' => 'u', 'Ü' => 'u', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'µ' => 'u',
'Œ' => 'oe', 'œ' => 'oe',
'ñ' => 'n',
'$' => 's');
$str = str_replace(" ", "_",$str);
$str = strtr($str,$caracteres);
echo $str // Piña_Colada
return $str;
}

...

while($row=mysql_fetch_object($requete))
        {

                $titre_recette = $row->titre; //with $row->titre = "Piña Colada"
                $titre_recette_photo = remove_acc($titre_recette);

                echo $titre_recette_photo;
        }

It shows "Piña_Colada" for some reasons... any solution ...?

Here we can see that it's working with declared $string : http://codepad.viper-7.com/psDpwb

it comes from a DB, so might it be encoded in a different way ? In notepad++, im working in utf-8 without BOM

Here is my table declaration :

$sql = "CREATE TABLE IF NOT EXISTS Recettes (
    id_recette int(11) DEFAULT NULL,
    titre varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL";
Niko
  • 35
  • 7
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 17 '14 at 22:42
  • can you echo the str before the return? – Fabio Cardoso Nov 17 '14 at 22:45
  • Yea, i know you re right about prepared statements and PDO, i'm currently learning it. this project is just an offline stuff anyway... – Niko Nov 17 '14 at 22:46
  • @fabioCardoso -> Piña_Colada – Niko Nov 17 '14 at 22:47

1 Answers1

0

Can you try it with decoding or encoding the string to UTF-8?

I've added this line to your function: $str = utf8_encode($str); // or maybe utf8_decode($str);

function remove_acc($str){
$caracteres = array(
'À' => 'a', 'Á' => 'a', 'Â' => 'a', 'Ä' => 'a', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ä' => 'a', '@' => 'a',
'È' => 'e', 'É' => 'e', 'Ê' => 'e', 'Ë' => 'e', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', '€' => 'e',
'Ì' => 'i', 'Í' => 'i', 'Î' => 'i', 'Ï' => 'i', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'Ò' => 'o', 'Ó' => 'o', 'Ô' => 'o', 'Ö' => 'o', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'ö' => 'o',
'Ù' => 'u', 'Ú' => 'u', 'Û' => 'u', 'Ü' => 'u', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'µ' => 'u',
'Œ' => 'oe', 'œ' => 'oe',
'ñ' => 'n',
'$' => 's');
$str = str_replace(" ", "_",$str);
$str = utf8_encode($str); // or maybe utf8_decode($str);
$str = strtr($str,$caracteres);
echo $str // Piña_Colada
return $str;
}
gfcodix
  • 181
  • 5
  • Well, it worked with $str = utf8_encode($str); like a charm... i thought i ve tried it already. i feel stupid for my question then haha! thanks – Niko Nov 17 '14 at 23:18