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!!! Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/ 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); Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/#findComment-542672 Share on other sites More sharing options...
[email protected] 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 Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/#findComment-542769 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. Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/#findComment-543363 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 Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/#findComment-543448 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); Link to comment https://forums.phpfreaks.com/topic/105893-matching-more-than-one-space/#findComment-543707 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.