I am writing a custom blog script and as part of it am using a slug for post URLs. The code I have for it so far is
public function get_slug($str) {
// convert to lowercase
$str = strtolower($str);
// remove special chars
$str = preg_replace('/[^a-zA-Z0-9]/i', ' ', $str);
// remove what space from beginning and end
$str = trim($str);
// remove repeat spaces
$str = preg_replace('/\s+/', ' ', $str);
// replace spaces with hyphens
$str = preg_replace('/\s+/', '-', $str);
return $str;
}
And this works great for most parts, however I have noticed if there is a special char in the middle of the word it simply replaces it with a hyphen turning 'can't' into 'can-t' instead of 'cant'
Whilst at preset I can just edit the db and fix it by hand, I would like to have it automatically just strip special chars from the middle of words and not replace them with a hypen.