0

I have run into a strange situation while using PHP's iconv() function. I have a MySQL database which contains several records that have French (and other European languages) characters within them. For example:

Saint-Côme-du-Mont

I am trying to write a function to convert these to friendly URLs, and turned to iconv(). The problem is that if I use the transliteration feature, I get the following error:

Notice: iconv(): Detected an illegal character in input string in...

The code generating the problem is:

$url = iconv("utf-8", "us-ascii//TRANSLIT", $string); // TRANSLIT does the whole job

However, if I use //IGNORE, I get the following output:

Saint-Cme-du-Mont 

The field in the MySQL uses `` collation, and is stored as plain text. I've tried using utf8_encode(), but the same behaviour is present.

If I use mb_detect_encoding() on the input string, I get UTF-8, so it seems to be receiving a string in the correct format.

How can I circumvent the Notice, and still carry out the translit operation?

BenM
  • 52,573
  • 26
  • 113
  • 168
  • 3
    It seems $string doesn't contain "Saint-Côme-du-Mont" as UTF-8. I tried your line of code completely as UTF-8 and got `Saint-C^ome-du-Mont` as result. – VMai Sep 03 '14 at 21:32
  • If I use `mb_detect_encoding()` on the input string, I get `UTF-8`, so it seems to be receiving a string in the correct format. – BenM Sep 03 '14 at 21:34
  • So mb_detect_encoding() is wrong. I get your error message while using "Saint-Côme-du-Mont" encoded in ISO-8859-1 and using your line of code, but it worked with `$url = iconv("ISO-8859-1", "us-ascii//TRANSLIT", $string);`, so I would give it a try instead. – VMai Sep 03 '14 at 21:38
  • I did try it: `mb_detect_encoding('Saint-Côme-du-Mont');` got wrongly UTF-8 while it's ISO-8859-1 as result. – VMai Sep 03 '14 at 21:41
  • You should have a good look at all parts of your code. If you're assuming that you've got UTF-8 but get ISO-8859-1 instead then the following output chain may produce results you don't desire. It's not only your url there. It should be wrong in the delivered content too. – VMai Sep 03 '14 at 21:48

0 Answers0