1

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

TVA van Hesteren
  • 1,031
  • 3
  • 20
  • 47

1 Answers1

2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563