PHP Scripts Development » General Web Development Forum

Convert string to slug

(1 post)
  • Started 1 month ago by Olaf

Great offers not only for geeks!


  1. Today I needed (fast) a function to convert a company name into a slug which I use as a part of the URL to a customers detail page. Since those names (> 1300 existing records) contains special characters like "é" or "ë", I needed a function that can handle this too.

    After some quick research on Google I found this snippet:

    function slug($str) {
    	$str = strtolower(trim($str));
    	$str = preg_replace('/[^a-z0-9-]/', '-', $str);
    	$str = preg_replace('/-+/', "-", $str);
    	return $str;
    }

    This function is very short and quick, I replaced the row with "return" to remove a trailing dash with this code:

    return rtrim($str, '-');

    Great function! Thanks Snipplr.com

    Posted 1 month ago #

RSS feed for this topic

Reply

You must log in to post.