Search code examples
phpslugslugify

Error while Creating title Slug Notice: iconv(): Detected an illegal character in input string in


I used this code from Stackoverflow to creat a title slug from a string

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;
 } 

It is working fine for most of the strings. but getting this for some strings like "Азур и Азмар / Azur et Asmar". what should i change now?

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

Solution

  • Just try changing

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

    to

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