-1

When I try to remove multiple spaces from a string by using this code

preg_replace('/\s{2,}/', ' ', $data)
I notice that also line breaks are removed.

When I use a space character in the syntax instead of a \s, then everything works normally.

So I'm wondering why do we have this behavior and what is happening?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user3292788
  • 407
  • 1
  • 6
  • 15

1 Answers1

1

\s matches a whitespace character. From PCRE:

The default \s characters are HT (9), LF (10), VT (11), FF (12), CR (13), and space (32), which are defined as white space in the "C" locale.

Emphasis mine.

noahnu
  • 3,479
  • 2
  • 18
  • 40