Page 1 of 1

collapsing spaces

Posted: Sat Feb 28, 2009 9:11 pm
by oboedrew
I'm trying to write a regex to collapse consecutive spaces when text is submitted through a form, while leaving consecutive newline characters intact. So far, I've got this:

preg_replace('|^ +$|', ' ', $text)

Seems like it ought to take one or more consecutive spaces and replace them with just a single space, but it doesn't work. Can anyone explain why?

Thanks,
Drew

Re: collapsing spaces

Posted: Sat Feb 28, 2009 9:37 pm
by John Cartwright

Code: Select all

$input = preg_replace('# {2,}#', ' ', $input);
Will replace 2 or more consequetive spaces with a single space.

Re: collapsing spaces

Posted: Sat Feb 28, 2009 10:40 pm
by oboedrew
Thanks, John. Is the main advantage of {2,} over + that the former will make the script run faster, as it only deals with two or more consecutive spaces, whereas the latter would've gone to work every time it encountered even a single space?

Also, I must be misunderstanding the use of ^ and $. Neither '|^ +$|' nor '|^ {2,}$|' work, but both '| +|' and '| {2,}|' work. I had thought that the ^ and $ would prevent preg_replace from replacing a larger string that happens to contain two or more consecutive spaces, but I must be mistaken. Can someone explain this to me?

Thanks,
Drew

Re: collapsing spaces

Posted: Sat Feb 28, 2009 10:46 pm
by John Cartwright
oboedrew wrote:Thanks, John. Is the main advantage of {2,} over + that the former will make the script run faster, as it only deals with two or more consecutive spaces, whereas the latter would've gone to work every time it encountered even a single space?
That is correct.
oboedrew wrote:Also, I must be misunderstanding the use of ^ and $. Neither '|^ +$|' nor '|^ {2,}$|' work, but both '| +|' and '| {2,}|' work. I had thought that the ^ and $ would prevent preg_replace from replacing a larger string that happens to contain two or more consecutive spaces, but I must be mistaken. Can someone explain this to me?

Thanks,
Drew
^ and $ are used to define the boundaries of the regex. ^ means from the beginning, $ means the end. In this case they are not needed, and will not be desirable because we will have more than just spaces from beginning to end, therefore the pattern would never match.

Re: collapsing spaces

Posted: Sun Mar 01, 2009 10:12 am
by oboedrew
Many thanks for the explanations, John!

Cheers,
Drew