Skip to content

Instantly share code, notes, and snippets.

@shayanabbas
Last active December 20, 2015 17:09
Show Gist options
  • Save shayanabbas/6166967 to your computer and use it in GitHub Desktop.
Save shayanabbas/6166967 to your computer and use it in GitHub Desktop.
Replaces all spaces and stupid character from a string, this function can be used while creating SEO url.
function safename($title){
// replaces every stupid character form a string with - ;
$title = str_replace("&", "and", $title);
$arrStupid = array('.com', '(tm)', ' ', '*', "'s", '"', ",", ":", ";", "@", "#", "(", ")", "?", "!", "_",
"$","+", "=", "|", '/', "~", "`s", "`", "\\", "^", "[","]","{", "}", "<", ">", "%", "™");
$title = htmlentities($title);
$title = preg_replace('/&([a-zA-Z])(.*?);/','$1',$title); // get rid of bogus characters
$title = strtolower("$title");
$title = str_replace(".", "", $title);
$title = str_replace($arrStupid, "-", $title);
$flag = 1;
while($flag){
$newtitle = str_replace("--","-",$title);
if($title != $newtitle) {
$flag = 1;
}
else $flag = 0;
$title = $newtitle;
}
$len = strlen($title);
if($title[$len-1] == "-") {
$title = substr($title, 0, $len-1);
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment