networkthis Posted May 16, 2008 Share Posted May 16, 2008 How can I make an regular expression that will take a message and match more then one blank space and replace it with just one blank space??? Thanks in advance!!! Quote Link to comment Share on other sites More sharing options...
priti Posted May 16, 2008 Share Posted May 16, 2008 this can be helpful preg_replace("\s+", "", $text); Quote Link to comment Share on other sites More sharing options...
damien@damosworld.com Posted May 16, 2008 Share Posted May 16, 2008 One minor change, replace match with one space instead of nothing Its also worth noting that technically this replace carriage returns as well. This will make everything appear on one line. Is this what you are wanting or is it simply just space characters that need replacing? preg_replace("\s+", " ", $text); Damien Quote Link to comment Share on other sites More sharing options...
networkthis Posted May 17, 2008 Author Share Posted May 17, 2008 Would like to replace just the space characters. Quote Link to comment Share on other sites More sharing options...
priti Posted May 17, 2008 Share Posted May 17, 2008 One minor change, replace match with one space instead of nothing Its also worth noting that technically this replace carriage returns as well. This will make everything appear on one line. Is this what you are wanting or is it simply just space characters that need replacing? preg_replace("\s+", " ", $text); Damien thanks damien for correcting me Quote Link to comment Share on other sites More sharing options...
corbin Posted May 17, 2008 Share Posted May 17, 2008 The space character in regular expressions is simply a space. (Well there are other ways to represent it, but that's the easiest way without lumping in other characters.) preg_replace('/[ ]+/', ' ', $text); Quote Link to comment 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.