Search code examples
phpstringurlurl-rewritingiconv

Replacing special characters from URL with iconv doesn't work


I have a small problem with some of my urls. Let's say that $result['title'] = Citroën

In my url, I want this word to become "citroen". The following function does everything right, except that it removes the "ë", so my url becomes "citron".

<?php echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $result['title'])));?>

I thought I could solve this by using iconv... but it doesn't work. "Citroën" is still replaced by "citron".

<?php echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $result['title']))));?>

So, what am I missing here?


Solution

  • Okay, I figured it out. I need to set a target locale. The following code works (so "Citroën" becomes "citroen"):

    <?php setlocale(LC_ALL, 'en_GB.utf8'); echo strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $result['titel']))));?>