1

This function for creating URL slugs:

function slugify($text)
{
  // replace non letter or digits by -
  $text = preg_replace('~[^\pL\d]+~u', '-', $text);

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  // trim
  $text = trim($text, '-');

  // remove duplicate -
  $text = preg_replace('~-+~', '-', $text);

  // lowercase
  $text = strtolower($text);

  if (empty($text)) {
    return 'n-a';
  }

  return $text;
}

doesn't replace characters like:

ľščťýžťžýéíáý

to something like:

lsctyztzyeiay

but instead, it removes them completely

so this string:

asdf 1234 3 ľščťlkiop

becomes

asdf-1234-3-lkiop

instead of:

asdf-1234-3-lsctlkiop

Any idea what is causing the disappearance of non-English characters and how to make them convert to the English variant?

John Doeherskij
  • 2,269
  • 3
  • 13
  • 19

1 Answers1

1

I tested your function and it seems to be working properly.
You can check here the output with your data.

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
  • Not working. if I do var_dump I am getting this: `ÄšťľšÄťľšÄťľšÄÅ¥` Any idea why? – John Doeherskij Jul 26 '16 at 17:11
  • You must set the encoding of the web page to UTF-8 to properly display special non-ASCII characters. Add `` in the `` block of your web page. – Jocelyn Jul 26 '16 at 17:14
  • You are right, it works in the view, but when saving into the db it is messed up. I will check some htmlentities or something before saving into the db. Thanks. – John Doeherskij Jul 26 '16 at 17:24
  • Read this, it will help: [How to support UTF-8 completely in a web application](http://stackoverflow.com/questions/279170/how-to-support-utf-8-completely-in-a-web-application) – Jocelyn Jul 26 '16 at 17:29