Function to generate an seo friendly url from the supplied string
function clean_url($untouched_str){
$cleaned_str = preg_replace('/\%/',' percentage',$untouched_str); //replace '%' with 'percentage'
$cleaned_str = preg_replace('/\@/',' at ',$cleaned_str); //replace '@' with 'at'
$cleaned_str = preg_replace('/\&/',' and ',$cleaned_str); //replace '&' with 'and'
$cleaned_str = preg_replace('/\s[\s]+/','-',$cleaned_str); // Strip off multiple spaces
$cleaned_str = preg_replace('/[\s\W]+/','-',$cleaned_str); // Strip off spaces and non-alpha-numeric
$cleaned_str = preg_replace('/^[\-]+/','',$cleaned_str); // Strip off the starting hyphens
$cleaned_str = preg_replace('/[\-]+$/','',$cleaned_str); // Strip off the ending hyphens
$cleaned_str = strtolower($cleaned_str); //convert to lowercase
return $cleaned_str;
}
usage:
echo( clean_url("Test url!@@ "));
the result will be like :
test-url
function clean_url($untouched_str){
$cleaned_str = preg_replace('/\%/',' percentage',$untouched_str); //replace '%' with 'percentage'
$cleaned_str = preg_replace('/\@/',' at ',$cleaned_str); //replace '@' with 'at'
$cleaned_str = preg_replace('/\&/',' and ',$cleaned_str); //replace '&' with 'and'
$cleaned_str = preg_replace('/\s[\s]+/','-',$cleaned_str); // Strip off multiple spaces
$cleaned_str = preg_replace('/[\s\W]+/','-',$cleaned_str); // Strip off spaces and non-alpha-numeric
$cleaned_str = preg_replace('/^[\-]+/','',$cleaned_str); // Strip off the starting hyphens
$cleaned_str = preg_replace('/[\-]+$/','',$cleaned_str); // Strip off the ending hyphens
$cleaned_str = strtolower($cleaned_str); //convert to lowercase
return $cleaned_str;
}
usage:
echo( clean_url("Test url!@@ "));
the result will be like :
test-url
Comments
Post a Comment