TVA van Hesteren
TVA van Hesteren

Reputation: 1221

How to replace all spaces which are not actually spaces with regex in PHP

I have the following string which I want to 'clean' from multiple whitespaces:

$string = "This is   a test string"; //Using utf8_decode

Not a big deal right? However, the string is not 'cleaned' after using:

$string = preg_replace('/\s+/', ' ', $string);

Because, the string actually is like this:

$test = "This is a  test string";

So, how can I fix this issue?

Thanks.

Please, I don't want to replace the  character like str_replace('Â', '') or something

Upvotes: 1

Views: 325

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use the /u UNICODE modifier:

$string = preg_replace('/\s+/u', ' ', $string);

The /u modifier enables the PCRE engine to handle strings as UTF8 strings (by turning on PCRE_UTF8 verb) and make the shorthand character classes in the pattern Unicode aware (by enabling PCRE_UCP verb)

The main point is that \s will now match all Unicode whitespace and the input string is treated as a Unicode string.

Upvotes: 2

Related Questions