A programming & designing blog!

Thursday, March 7, 2013

Clean URL Slug From Ugly String Using PHP

Google strongly guide webmasters to make there website URLs SEO friendly or Google friendly. Creating URL from title is the best way to make URLs more user or search engine friendly. This simple function can make clean and friendly URL slug from ugly to uglier title or string.

The URL Slug Function:
 * URL Slug
 * @param str $str
 * @return str
function url_slug($str)
	#convert case to lower
	$str = strtolower($str);
	#remove special characters
	$str = preg_replace('/[^a-zA-Z0-9]/i',' ', $str);
	#remove white space characters from both side
	$str = trim($str);
	#remove double or more space repeats between words chunk
	$str = preg_replace('/\s+/', ' ', $str);
	#fill spaces with hyphens
	$str = preg_replace('/\s+/', '-', $str);
	return $str;

$title = '  This -iS- (my)   @$ugly@#          tiTle !!!!!!!!!!!!!!!!!!!! ';
echo url_slug($title);



  1. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area. php training in chennai

  2. It is a very nice article including a lot of viral content. I am going to share it on social media. Get the Cold Pressed Oil in online.

  3. These provided information was really so nice, thanks for giving that post and the more skills to develop after refer that post.
    Best cloud Computing Training Institute in Chennai | Best cloud Computing Training Institute in Velachery