PHP: replacing multiple spaces with one space


See the following code:

$str = 'This    string    has   many   spaces';
$str = preg_replace('/\s+/', ' ', $str);
print $str;

output

This string has many spaces