Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
share|improve this question
up vote 1 down vote accepted

An alternative, simpler way to code your solution is to use the strtr function which "translates characters". Also I made sure to escape the special characters in the regex.

function url_clean($str) {
    $accent = array(' űáéúőóüöíŰÁÉÚŐÓÜÖÍ');
    $clean  = array('-uaeuoouoiUAEUOOUOI');
    $str = strtr($str, $accent, $clean);
    return preg_replace('/[^A-Za-z0-9\-\.]/', '', $str);
share|improve this answer
thank you, this is what i searching for – eaposztrof Feb 22 '13 at 22:45

There are two issues with your otherwise elegant approach:

  1. iconv silently cuts the string if a disallowed UTF-8 character is present. The solution would be to add //IGNORE to the iconv() call but 1/ a bug in glibc seems to prevent this 2/ PHP developers don't seem to want to implement a work-around. An option is to remove invalid characters yourself:

    ini_set('mbstring.substitute_character', "none"); 
    $text= mb_convert_encoding($text, 'UTF-8', 'UTF-8'); 
  2. You're not removing all characters that are present in ASCII but disallowed in a URL: see this StackOverflow answer.

share|improve this answer

Thank you, finally this is my solution:

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);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.