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 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, '-'); 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, '-'); Link to comment https://forums.phpfreaks.com/topic/258327-little-preg_replace-trouble/#findComment-1324193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.