Danny620 Posted May 29, 2012 Share Posted May 29, 2012 I have user submitted data like: repairs,national, companies, larger,cheaper, large, trade, windscreen , as you can see its not consistent in some cases it has a space after the comma and then it has more spaces, what I'm asking for is a preg_replace expression that would tidy the data to end up like this: repairs, national, companies, larger, cheaper, large, trade, windscreen so 1 space after the comma, and no comma on end of string thanks Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/ Share on other sites More sharing options...
ManiacDan Posted May 29, 2012 Share Posted May 29, 2012 $theString = trim(preg_replace("/\s*,\s*/", ", ", $theString), ','); Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349524 Share on other sites More sharing options...
Danny620 Posted May 29, 2012 Author Share Posted May 29, 2012 im using this to remove the comma on end of string however its not working echo $theString = 'repairs,national, companies, larger,cheaper, large, trade, windscreen ,'."<br />"; echo $theString = trim(preg_replace("/\s*,\s*/", ", ", $theString))."<br />"; echo $theString = rtrim( $theString, ',' ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349541 Share on other sites More sharing options...
ManiacDan Posted May 29, 2012 Share Posted May 29, 2012 You put a <br> at the end of the string. Don't do that. Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349543 Share on other sites More sharing options...
Danny620 Posted May 29, 2012 Author Share Posted May 29, 2012 it still does not remove the last comma from the end of the string Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349544 Share on other sites More sharing options...
Danny620 Posted May 29, 2012 Author Share Posted May 29, 2012 sorted just changed trim(preg_replace("/\s*,\s*/", ", ", $theString), ',spacehere needed'); Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349548 Share on other sites More sharing options...
ManiacDan Posted May 29, 2012 Share Posted May 29, 2012 Then you were adding a space at the end of the string too. I can only work with what I was given. Your string ended in a comma, not a space or a <br> Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349565 Share on other sites More sharing options...
Danny620 Posted May 29, 2012 Author Share Posted May 29, 2012 Yes sorry about that thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/263329-regexr-help/#findComment-1349621 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.