I try to get rid of some characters in a string. The string will be submited by a form (method: POST). I need to get a lower case string. The Characters ä, ö, ü and ß should become ae, oe, ue and ss. All other special characters and whitespaces should be removed.
The string
Lorem_ipsum_Ö-ß &änyOther char should become
lorem-ipsum-oe-ss-aenyother-char This is my code:
$slug = strtolower($_POST['title']); $slug = trim($slug, '-'); $slug = preg_replace('/ä/g', 'ae', $slug); $slug = preg_replace('/ö/g', 'oe', $slug); $slug = preg_replace('/ü/g', 'üe', $slug); $slug = preg_replace('/ß/g', 'ss', $slug); $slug = preg_replace('/[^\w ]/g', '', $slug); $slug = preg_replace('/ +/g', '-', $slug); First of all, my attempt doesn't work as I want to.
And second: How can I make this code smaller? I guess it is possible to get this in just one regular expression...
/gmodifier forpreg_match, remove it, will work fine.Öwill not transform to lowercase using strtolower()