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
rtrim()to remove it.
If you are dealing with extra spaces within a string consider a
preg_replaceof multiple whitespaces
" "*with a single whitespace
$foo = preg_replace('/s+/', ' ', $foo);