Not sure exactly what you want but here are two situations:
-
If you are just dealing with excess whitespace on the beginning or end of the string you can use
trim(),ltrim()orrtrim()to remove it. -
If you are dealing with extra spaces within a string consider a
preg_replaceof multiple whitespaces" "*with a single whitespace" ".
Example:
$foo = preg_replace('/\s+/', ' ', $foo);