en
Home About project

Replace multiple spaces in a string to single space with PHP

If you need to replace multiple spaces (double spaces) in a string to single space with PHP you can use next string function:

  • mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int &$count])

This function searches subject for matches to pattern and replaces them with replacement.

To replace double spaces to single space using PHP see example:

    $string = preg_replace('/\s/', ' ', $string);