"remove excess whitespace from within a string" Code Answer

4

Not sure exactly what you want but here are two situations:

  1. If you are just dealing with excess whitespace on the beginning or end of the string you can use trim(), ltrim() or rtrim() to remove it.

  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);
By user336786 on October 5 2022

Answers related to “remove excess whitespace from within a string”

Only authorized users can answer the search term. Please sign in first, or register a free account.