Create Slug for a string

Regular expression function that replaces spaces & special characters between words with hyphens


function create_slug($string) {
	// removing special characters from the string
	$slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
	// removing multiple - generated in slug
	$slug = preg_replace("/[-]+/", '-', $slug);
	// lower case slug string
	$slug = strtolower($slug);
	// removing - from the first & last place if any
	$slug = trim($slug, "-");
	return $slug;
}

echo create_slug(" This ~` is ?><:{}|][;/.,]+_)(*&^%$#@!) 'MY TITLE' for------this post. ");