How to generate search engine friendly slug using PHP

The following function generates a search friendly slug for your page/post/article. You need to pass your page title, article title, post title etc. to it and it will generate a lowercase search engine string separated by hyphen “-“. Let me know if this function helped you in any way. Always happy to help others!

function generateSlug($phrase) {
	$str = strtolower($phrase);
	$str = preg_replace("/[^a-z0-9\s-]/", "", $str);
	$str = trim(preg_replace("/[\s-]+/", " ", $str));
	$str = preg_replace("/\s/", "-", $str);
	return $str;
}
Please follow and like us:

3 Comments

Leave a Reply

Your email address will not be published.


*