strago Posted March 5, 2012 Share Posted March 5, 2012 $titleurl = preg_replace('/[^a-zA-Z0-9_ -%]/', '-', $d); $titleurl = preg_replace('/[ ]/', '-', $titleurl); How do you add to this to replace ---- with - --- with - -- with - and remove quotes? How do you remove a - if it's at the very end of $titleurl. For example, replace My-iPhone- with My-iPhone Quote Link to comment https://forums.phpfreaks.com/topic/258327-little-preg_replace-trouble/ Share on other sites More sharing options...
Adam Posted March 5, 2012 Share Posted March 5, 2012 You can have a simple pattern match one or more hyphens and replace with a single hyphen: $str = preg_replace('/-+/', '-', $str); Then to remove any leading or trailing hyphens, just run it through trim: $str = trim($str, '-'); Quote Link to comment https://forums.phpfreaks.com/topic/258327-little-preg_replace-trouble/#findComment-1324191 Share on other sites More sharing options...
Adam Posted March 5, 2012 Share Posted March 5, 2012 Sorry, missed "adapt this" - $titleurl = preg_replace('/[^a-zA-Z0-9_ -%]/', '-', $d); $titleurl = preg_replace('/-+/', '-', $titleurl); $titleurl = trim($titleurl, '-'); Quote Link to comment https://forums.phpfreaks.com/topic/258327-little-preg_replace-trouble/#findComment-1324193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.