Create URL Slug from Post Title

Regular expression function that replaces spaces between words with hyphens.

<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>

Comments

  1. User Avatar
    Robin
    Permalink to comment#

    Maybe you can add a strtolower there?

  2. User Avatar
    Dalibor Sojic
    Permalink to comment#

    Here is jquery (client side) slug creator.

    $('#title').change(function() {
    $(this).val($.trim($(this).val()));
    // Trim empty space

    $(this).val($(this).val().replace(/\s+/g,' '));
    // replace more then 1 space with only one

    $('#url').val($(this).val().toLowerCase());
    $('#url').val($('#url').val().replace(/\W/g, ' '));
    $('#url').val($.trim($('#url').val()));
    $('#url').val($('#url').val().replace(/\s+/g, '-'));
    });

    • User Avatar
      Micheal Johnson
      Permalink to comment#

      Don’t use a client-side slug creator, as it can be maliciously exploited. If an attacker wants to insert special characters into a slug for whatever reason, they can easily bypass the client-side script and send the malicious slug to the server. If the server doesn’t perform any additional checking/modification on the submitted slug (which if it did then a client-side slug creator isn’t necessary), then the attacker can successfully exploit the server.

  3. User Avatar
    Benjamin Mayo
    Permalink to comment#

    Stop words are always a nice accompaniment to slug creators,

  4. User Avatar
    Weblap.ro
    Permalink to comment#

    function remove_accent($str)
    {
    $a = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','Ā','ā','Ă','ă','Ą','ą','Ć','ć','Ĉ','ĉ','Ċ','ċ','Č','č','Ď','ď','Đ','đ','Ē','ē','Ĕ','ĕ','Ė','ė','Ę','ę','Ě','ě','Ĝ','ĝ','Ğ','ğ','Ġ','ġ','Ģ','ģ','Ĥ','ĥ','Ħ','ħ','Ĩ','ĩ','Ī','ī','Ĭ','ĭ','Į','į','İ','ı','IJ','ij','Ĵ','ĵ','Ķ','ķ','Ĺ','ĺ','Ļ','ļ','Ľ','ľ','Ŀ','ŀ','Ł','ł','Ń','ń','Ņ','ņ','Ň','ň','ʼn','Ō','ō','Ŏ','ŏ','Ő','ő','Œ','œ','Ŕ','ŕ','Ŗ','ŗ','Ř','ř','Ś','ś','Ŝ','ŝ','Ş','ş','Š','š','Ţ','ţ','Ť','ť','Ŧ','ŧ','Ũ','ũ','Ū','ū','Ŭ','ŭ','Ů','ů','Ű','ű','Ų','ų','Ŵ','ŵ','Ŷ','ŷ','Ÿ','Ź','ź','Ż','ż','Ž','ž','ſ','ƒ','Ơ','ơ','Ư','ư','Ǎ','ǎ','Ǐ','ǐ','Ǒ','ǒ','Ǔ','ǔ','Ǖ','ǖ','Ǘ','ǘ','Ǚ','ǚ','Ǜ','ǜ','Ǻ','ǻ','Ǽ','ǽ','Ǿ','ǿ');
    $b = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o');
    return str_replace($a, $b, $str);
    }

    function post_slug($str)
    {
    return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($str)));
    }

  5. User Avatar
    Ant

    You would need also to remove punctuation symbols:

    $string = preg_replace( '/[«»“”!?,.]+/', '', $string );
  6. User Avatar
    Asif Iqbal
    Permalink to comment#

    Its quite simple and good. Nice effort I must say.. I had also written one post for ASP Classic for same topic.

    How to: Create SEO Friednly URL in ASP Classic

    Thanks!

  7. User Avatar
    Sudeep Sakalle
    Permalink to comment#

    Excellent post ! very simple and good.

    Thanks for putting it online.

    @Weblap.ro Thanks for this special char. removal script. This makes this page very useful.

    Sudeep
    Infovinity

  8. User Avatar
    Seebz

    Another equivalent function that make better results : http://code.seebz.net/p/to-permalink/

  9. User Avatar
    itechfan
    Permalink to comment#

    how to make post slug when you language is utf-8 like “xin chào”. i don’t want to change like “xin cho” when i use your function.

    My function like you:

    
    function remove_accent($str) 
    { 
      $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', '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'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'); 
      return str_replace($a, $b, $str); 
    } 
    
    function post_slug($str) 
    { 
      return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), 
      array('', '-', ''), remove_accent($str))); 
    } 
    

    Help me make change slug like string: “xin chào.”

    Thanks

    • User Avatar
      Vincent Le
      Permalink to comment#

      For Vietnamese language I used this array:

      function remove_accent($str) 
      { 
        $a = array(
                      'á','é','í','ó','ú','ý','Á','É','Í','Ó','Ú','Ý',
                      'à','è','ì','ò','ù','ỳ','À','È','Ì','Ò','Ù','Ỳ',
                      'ả','ẻ','ỉ','ỏ','ủ','ỷ','Ả','Ẻ','Ỉ','Ỏ','Ủ','Ỷ',
                      'ã','ẽ','ĩ','õ','ũ','ỹ','Ã','Ẽ','Ĩ','Õ','Ũ','Ỹ',
                      'ạ','ẹ','ị','ọ','ụ','ỵ','Ạ','Ẹ','Ị','Ọ','Ụ','Ỵ',
                      'â','ê','ô','ư','Â','Ê','Ô','Ư',
                      'ấ','ế','ố','ứ','Ấ','Ế','Ố','Ứ',
                      'ầ','ề','ồ','ừ','Ầ','Ề','Ồ','Ừ',
                      'ẩ','ể','ổ','ử','Ẩ','Ể','Ổ','Ử',
                      'ậ','ệ','ộ','ự','Ậ','Ệ','Ộ','Ự'
                  ); 
        $b = array(
                      'a','e','i','o','u','y','A','E','I','O','U','Y',
                      'a','e','i','o','u','y','A','E','I','O','U','Y',
                      'a','e','i','o','u','y','A','E','I','O','U','Y',
                      'a','e','i','o','u','y','A','E','I','O','U','Y',
                      'a','e','i','o','u','y','A','E','I','O','U','Y',
                      'a','e','o','u','A','E','O','U',
                      'a','e','o','u','A','E','O','U',
                      'a','e','o','u','A','E','O','U',
                      'a','e','o','u','A','E','O','U',
                      'a','e','o','u','A','E','O','U'
                  ); 
        return str_replace($a, $b, $str); 
      }
      
  10. User Avatar
    tranh
    Permalink to comment#

    cool tips, but I can restrics duplicate slug

  11. User Avatar
    Eric
    Permalink to comment#

    For the quick and not-so-dirty way I’ve always used:

    $slug = preg_replace(“/\W+/”,”-“,$text);

    By using \W+ you take care of all non-latin characters.

  12. User Avatar
    Thomas
    Permalink to comment#

    No matter what I need done on a website I’m having trouble with I google what I want and your links never disappoint. Thanks Chris!

  13. User Avatar
    Frenk
    Permalink to comment#

    How can i add a button? So when i click on Auto genrerator frienly url

  14. User Avatar
    Shelly
    Permalink to comment#

    What about the built-in function for this? sanitize_title_with_dashes(); I use it all the time.

    • User Avatar
      Rian Ariona
      Permalink to comment#

      sanitize_title_with_dashes() is wordpress function :).

  15. User Avatar
    Shelly
    Permalink to comment#

    My bad! I thought you all were talking about WordPress :) Sorry!

  16. User Avatar
    Davinder Singh
    Permalink to comment#
    function getSlug($text)
    { 
      $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
      $text = trim($text, '-');
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
      $text = strtolower($text);
      $text = preg_replace('~[^-\w]+~', '', $text);
      if (empty($text))
      {
        return 'n-a';
      }
      return $text;
    }
    

    100% working

    • User Avatar
      Pkumar
      Permalink to comment#

      my title is बोन कैंसर: जानें, क्यों नहीं नजरअंदाज करना चाहिए इन संकेतों को?
      i want to change into बोन-कैंसर-जानें-क्यों-नही.html
      not working with above code any suggestion.

  17. User Avatar
    Chirag
    Permalink to comment#

    Thanx its really worth and work for me…:p

  18. User Avatar
    Piotr Alexei
    Permalink to comment#

    Another way to do that simply :

    function text2url($string){
            $table = array(
                'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
                'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
                'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
                'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
                'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
                'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
                'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', '/' => '-', ' ' => '-'
            );
            $text2url = strtolower(strtr($string, $table));
            $text2url = preg_replace('#[^0-9a-z]+#i', " ", $text2url);
            $text2url = str_replace(' ','-',trim($text2url));
            return $text2url;
        }
    
  19. User Avatar
    Ladislav Janeček
    function slug($nadpis) {
        $url = $nadpis;
        $url = preg_replace('~[^&#092;pL0-9_]+~u', '-', $url);
        $url = trim($url, "-");
        $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
        $url = strtolower($url);
        $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
        return $url;
    }
    
  20. User Avatar
    AmirMasoud

    creating slug in this way is limited to English alphabets. it’s better to first use function explode and space as delimiter to convert to an array and then implode it with optional delimiter.

  21. User Avatar
    Stackoverflow
    Permalink to comment#
    public static function slug($string, $space="-") {
            $string = utf8_encode($string);
            if (function_exists('iconv')) {
                $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
            }
    
        $string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
        $string = trim(preg_replace("/\\s+/", " ", $string));
        $string = strtolower($string);
        $string = str_replace(" ", $space, $string);
    
        return $string;
    }
    
    function createPostSlug($url){
        # Prep string with some basic normalization
        $url = strtolower($url);
        $url = strip_tags($url);
        $url = stripslashes($url);
        $url = html_entity_decode($url);
    
        # Remove quotes (can't, etc.)
        $url = str_replace('\'', '', $url);
    
        # Replace non-alpha numeric with hyphens
        $url     = trim(preg_replace('/[^a-z0-9]+/', '-', $url), '-');
    
        return $url;
    

    }

  22. User Avatar
    Select

    This nice script will handle every aspect. Including removing special characters, and handling different languages

    https://github.com/cocur/slugify

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag