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