Stripping redundant whitespace -- whats wrong?

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

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Stripping redundant whitespace -- whats wrong?

Post by alex.barylski »

I have the following code:

Code: Select all

$value = 't    -   his     is a spaced line';
$value = preg_replace('#\s{2,}+#', ' ', $value);
All whitespace *except* that right at the start is being replaced with a single space why is the first not as well?
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Stripping redundant whitespace -- whats wrong?

Post by ridgerunner »

There isn't anything worng with your regex. I just copied and pasted your code into a php script and it works A-Ok. Also checks out in RegexBuddy.
Post Reply