Search code examples
phputf-8translationiconv

PHP Iconv adding extra characters


I'm trying to remove all the accents etc from some text in PHP using iconv. It strips the accents but sometimes adds an extra character, for example: "Théâtre" comes out as "Th'e^atre" whereas I want "Theatre"

Here is my code:

setlocale(LC_ALL, "fr_FR.utf8");
$text = "Théâtre";
$text = iconv("utf-8","ascii//TRANSLIT//IGNORE",$text);
echo $text;

Result: Th'e^atre


Solution

  • This error is probably due to a wrong implemantation of iconv() on your server (glibc instead of libiconv, you can check it in the output of phpinfo()). See here for more details.

    You can also try :

    $text = "Théâtre";
    $text = iconv("utf-8","ascii//TRANSLIT",$text); //$text = "Th'e^atre"
    //Replace any character not in alphabet by an empty substring.
    $text = preg_replace("#[^a-zA-z]#", "", $text); //$text = "Theatre"
    
    echo $text;