1

The title is self explanatory.

Currently I use preg_replace('/\s+/', ' ',$row['message']); but this removes newline characters as well, but I want to preserve them.

Dzhuneyt
  • 7,757
  • 13
  • 60
  • 112

1 Answers1

1

Use a double negated class, and negate \n. This is the perfect case to use !

preg_replace('/[^\S\n]+/', ' ', $row['message']);

Here is a regex demo.

Unihedron
  • 10,601
  • 13
  • 59
  • 69