Remove Excess Whitespace From Within a String
There is have two conditions :
-
If you are just dealing with excess whitespace on the beginning or end of the string you can use
-
trim()
Thetrim()
function removes whitespace and other predefined characters from both sides of a string. -
ltrim()
Removes whitespace or other predefined characters from the left side of a string -
rtrim()
Removes whitespace or other predefined characters from the right side of a string
-
-
If you are dealing with extra spaces within a string consider a
preg_replace
of multiple whitespaces" "*
with a single whitespace" "
.
Example:
$foo = preg_replace('/\s+/', ' ', $foo);