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
collapsing spaces
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: collapsing spaces
Code: Select all
$input = preg_replace('# {2,}#', ' ', $input);Re: collapsing spaces
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: collapsing spaces
That is correct.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?
^ 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 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
Re: collapsing spaces
Many thanks for the explanations, John!
Cheers,
Drew
Cheers,
Drew