cerruti
cerruti

Reputation: 121

Finding a certain white space?

I need to find a certain whitespace with regex. I need to find and replace whitespace or multiple whitespaces only if it's before or after a new line and the other whitespaces should remain.

Now i'm using this to remove multiple whitespaces:

preg_replace('/\s{2,}/', ' ', $string);

Thanks.

Upvotes: 2

Views: 116

Answers (1)

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67227

Just use the m (multiline) modifier so that ^ and $ match the start/end of a line. Then you can write a pattern like this:

preg_replace('/^\s+?|\s+?$/m' , ' ', $string);

Upvotes: 7

Related Questions