0

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

eapo
  • 1,053
  • 1
  • 19
  • 40
  • What if `$str` contains e.g. `%30` (or any other url-encoded character)? What if it contains characters invalid in a URL but part of ASCII? – Jon Feb 11 '13 at 13:13
  • 1
    Maybe this is better suited on [Code Review](http://codereview.stackexchange.com)? – insertusernamehere Feb 11 '13 at 13:17
  • Made for creating SEO friendly urls, in this case can't see the problem. But you are right. How can i make more efficient? – eapo Feb 11 '13 at 13:19
  • I woudn't rely on iconv to do proper transliteration. Use [urlify](https://github.com/jbroadway/urlify) or [see this](http://www.php.net/manual/en/transliterator.transliterate.php#110598) – nice ass Feb 11 '13 at 13:27
  • @OneTrickPony i think these are easy and great solutions, but on a webpage (unlike portals or cms sites) doesn't needed so many resource use. Am i rigth? – eapo Feb 12 '13 at 00:26

2 Answers2

1

Your previous function is much better than the one in the response (more cases). The problem is with the locale. Try to add the next line before the function definition:

setlocale(LC_ALL, 'es_ES.UTF8');

(es_ES is my locale, but yours may be different).

Ivan
  • 14,692
  • 17
  • 59
  • 96
0

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);
}
eapo
  • 1,053
  • 1
  • 19
  • 40