Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Replace Multiple White Spaces With a Single Space

The following snippet will remove double white space, tabs, and line breaks from a string.

1
$output = preg_replace('!\s+!', ' ', $input);

\d, \w and \s

Shorthand character classes matching digits, word characters (letters, digits, and underscores), and whitespace (spaces, tabs, and line breaks). Can be used inside and outside character classes. \s is shorthand for [\t\n\r].

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment