views:

1294

answers:

3

I get a string from a db, then I remove all the HTML tags, carriage returns and newlines, before I put it in a csv. Only thing is I cant remove the excess white space from between the strings, any ideas?

Thanks,

Joe

A: 
$str = str_replace(' ','',$str);

Or, replace with underscore, & nbsp; etc etc.

Cory Dee
This removes all whitespace. He just wants to normalise the string.
Svend
+4  A: 

Not sure exactly what you want but here are two situations

  1. If you just are dealing with excess whitespace on the beginning or end of the string you can use trim() ltrim() or rtrim() to remove that.

  2. If you are dealing with extra spaces within a string consider a preg_replace of multiple whitespaces " "* with a single whitespace " "

    $foo = preg_replace( '/\s+/', ' ', $foo );

Thanks genio!

jW
$foo = preg_replace( '/\s+/', ' ', $foo );
genio
A: 

$str=preg_replace('/[\s]+/',' ',$str);

Sandip Layek