Hi, I have a function to change my urls to seo urls however It's changing: - to nothing. I want it to keep the dash unchanged, what's preventing this? return str_replace(' ', '-', preg_replace('/([^a-z0-9\s]+)/i', '', trim($text))); PHP:
In your preg_replace you don't include the hyphen as one of the allowed characters. You only allow letters, numbers, and whitespace.
Try this: return preg_replace('/([^a-z0-9-]+)/i', '', trim(str_replace(' ','-',$text)))); PHP: It: * Trims * replaces spaces by - * deletes all non alphanumeric charactars but leaves - I didn't run the script so please correct errors
Actually it does the opposite; see the ^ character near the beginning. Nabil's solution is closer but it will result in unsightly runs of hyphens in some cases. I didn't want to spoon feed, but try this: return str_replace(' ', '-', trim(preg_replace('/[^a-z0-9]+/i', ' ', $text))); Code (markup):