Press "Enter" to skip to content

Remove Excess Whitespace From Within a String

0

Remove Excess Whitespace From Within a String

 

There is have two conditions :

  1. If you are just dealing with excess whitespace on the beginning or end of the string you can use 

    • trim() The trim() 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

  2. 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);