Search code examples
phpurlseoiconvclean-urls

PHP creating slugify (clean URL) links in simple way? iconv malfunction?


I think this is the simplest way to slugify urls. You have any contra-indication?

function url_clean($str)
{
   $str = iconv('utf-8', 'us-ascii//TRANSLIT', $str);
   $clean_str = preg_replace(array(
      '/\'|\"/',
      '/ /'
   ) , array(
      '',
      '-'
   ) , $str);
   return $clean_str;
}

UPDATE

the code above working great on local, but on the server return string with ? instead of transliterated characters: árvíztűrő -> ?rv?zt?r?

phpinfo() on localhost

iconv support enabled

iconv implementation "libiconv"

iconv library version 1.14

phpinfo() on server

iconv support enabled

iconv implementation glibc

iconv library version 2.12


Solution

  • Thank You! Finally this is my solution without that buggy iconv():

    function url_clean($str) {
        $accent = array(' ','ű','á','é','ú','ő','ó','ü','ö','í','Ű','Á','É','Ú','Ő','Ó','Ü','Ö','Í');
        $clean = array('-','u','a','e','u','o','o','u','o','i','U','A','E','U','O','O','U','O','I');
        $str = str_replace($accent, $clean, $str);
        return preg_replace('/[^A-Za-z0-9-.]/', '', $str);
    }