tomtimms Posted April 6, 2010 Share Posted April 6, 2010 I am trying to remove all the white space before and after a comma. So if I have ( Bob Thomas,Bill Sampson , Ted Ginn , Greg Mood ) it would then become (Bob Thomas,Bill Sampson,Ted Ginn,Greg Mood). I have tried using TRIM however that removed white space before and after the string. Link to comment https://forums.phpfreaks.com/topic/197753-remove-white-space-before-and-after-a-comma/ Share on other sites More sharing options...
Psycho Posted April 6, 2010 Share Posted April 6, 2010 preg_replace() to the rescue: $text = "Bob Thomas,Bill Sampson , Ted Ginn , Greg Mood"; $newText = preg_replace("/\s*,\s*/", ",", $text); echo $newText . "<br>"; //Output: Bob Thomas,Bill Sampson,Ted Ginn,Greg Mood Link to comment https://forums.phpfreaks.com/topic/197753-remove-white-space-before-and-after-a-comma/#findComment-1037787 Share on other sites More sharing options...
tomtimms Posted April 6, 2010 Author Share Posted April 6, 2010 Thanks, I actually tried using preg_replace before however I couldn't get the pattern right....seems like you were able to show me what I was missing...Thanks a ton! Link to comment https://forums.phpfreaks.com/topic/197753-remove-white-space-before-and-after-a-comma/#findComment-1037791 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.