Last active
December 20, 2015 17:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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