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
  • 8,437
  • 14
  • 64
  • 118

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,902
  • 13
  • 62
  • 72