0

This is my slugify function:

function slugify($text) {
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    $text = trim($text, '-');
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = mb_strtolower($text, 'UTF-8');
    $text = preg_replace('~[^-\w]+~', '', $text);
    if(empty($text)) return 'n-a';
    return $text;
}

This is the test:

echo slugify("espaƱa");

In my development server the result is:

  • espana

In my production server the result is:

  • espaa

I'm sure it has something to do with charset encoding, but both servers have UTF-8 as default_charset. What else could I be missing? Any ideas?

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
Andres SK
  • 10,779
  • 25
  • 90
  • 152

1 Answers1

3

The problem comes from the iconv function. In the comments of the documentation, we can see:

Please note that iconv('UTF-8', 'ASCII//TRANSLIT', ...) doesn't work properly when locale category LC_CTYPE is set to C or POSIX. You must choose another locale otherwise all non-ASCII characters will be replaced with question marks.

Community
  • 1
  • 1
E.Martinez
  • 91
  • 2
  • 5