I'm trying to remove excess whitespace from a string like this:
hello world
to
Anyone has any idea how to do that in PHP?
With a regexp :
preg_replace('/( )+/', ' ', $string);
If you also want to remove every multi-white characters, you can use s (s is white characters)
preg_replace('/(s)+/', ' ', $string);
2.1m questions
2.1m answers
60 comments
57.0k users