mitch95
5/26/2017 - 8:01 AM

Clean a string so it can be used in the URL. Can be used to create slug URLS for custom sites or WordPress

Clean a string so it can be used in the URL.

Can be used to create slug URLS for custom sites or WordPress

function clean($string) 
{
   	$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   	$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
   	$string = strtolower($string);
	  // Check there is no double/triple 
   	$string = preg_replace('/-+/', '-', $string);
   	$string = str_replace('---', '-', $string);
   	$string = str_replace('--', '-', $string);
   	$string = rtrim($string, '-');
   	$string = ltrim($string, '-');
   	return $string;
}