PHP - reduce multiple consecutive spaces to a single space

A PHP function that reduces multiple consecutive spaces in a string to one single space, so something like "   " is reduced to " ".

function reduceMultipleSpacesToSingleSpace($text) {
  return preg_replace('/\s+/', " ", $text );
}

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.