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.
Use a double negated class, and negate \n
. This is the perfect case to use regex-negation!
preg_replace('/[^\S\n]+/', ' ', $row['message']);
Here is a regex demo.