Removing Spaces from Text

Hello

I have a problem. In a text area, my users enter double triple or more spaces at BEGINING, MIDDLE of the PARAGRAPH AND/OR at the end.

Now, I want to know how I can remove those spaces (only 1 space should be there)

Note that I do not want to remove any NEW LINE Characters.

Please help

Zeeshan

With something like this?


$str = preg_replace ('/ +/', ' ', trim($str));

or if you don’t want to trim starting and ending spaces


$str = preg_replace ('/ +/', ' ', $str);


echo str_replace( "  ", " ", $var );

If you want to get rid of leading, trailing n spaces then use trim() on the string too.