real way to get rid of white space
Posted: Mon May 23, 2005 6:57 am
is there a real good way to get rid of extra whitespace within a string?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
i need a way to do it in the middle of the string but i have no idea exactly how much whitespace there is so str_replace really wouldnt be too efficientThis function returns a string with whitespace stripped from the beginning and end of str
Well the problem with trim is that it only trims whitespace from the start and end of the entire string. It wont trim stuff in the middle.
Code: Select all
$string = <<<EOD
La la la la
dumty dumty dum
I have many spaces in me! Tidy me up a bit
EOD;
$clean_string = preg_replace('/\s+/', '', $string); //Just make the second parameter a single space to turn excess whitespace into a single space