collapsing spaces

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

collapsing spaces

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: collapsing spaces

Post by John Cartwright »

Code: Select all

$input = preg_replace('# {2,}#', ' ', $input);
Will replace 2 or more consequetive spaces with a single space.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: collapsing spaces

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: collapsing spaces

Post 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.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: collapsing spaces

Post by oboedrew »

Many thanks for the explanations, John!

Cheers,
Drew
Post Reply