real way to get rid of white space
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
real way to get rid of white space
is there a real good way to get rid of extra whitespace within a string?
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
trim only works on the ends of a string but if my string is "bbbb (lots of whitespace) bbbb" then it wouldnt work on getting rid of the white space in the middle of the string.
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
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
I don't think there's a function to do it but if you want ALL whitespace (newlines, tabs, spaces etc) removing this does it.
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