229

I'm trying to replace multiple spaces with a single space. When I use ereg_replace, I get an error about it being deprecated.

ereg_replace("[ \t\n\r]+", " ", $string);

Is there an identical replacement for it. I need to replace multiple " " white spaces and multiple nbsp white spaces with a single white space.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Dani
  • 2,293
  • 2
  • 14
  • 4

3 Answers3

473

Use preg_replace() and instead of [ \t\n\r] use \s:

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

From Regular Expression Basic Syntax Reference:

\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.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • 1
    @Cletus: This one would replace a single space with space. Don't you think something like: preg_replace('/(?:\s\s+|\n|\t)/', ' ', $x) will be more *efficient* especially on text with several single spaces ? – codaddict Mar 03 '10 at 04:23
  • 30
    @codaddict: by chance, a moment ago i benchmarked those on real-life data, result (for calls on ~8300 various text articles): `/(?:\s\s+|\n|\t)/` => 1410 (slowest), `/\s+/` => 611 (ok'ish), `/\s\s+/` => 496 (fastest). The last one does not replace single `\n` or `\t`, but thats ok for my case. – Frunsi Dec 16 '11 at 02:09
  • 18
    /\s{2,}/u' - if you have some UTF-8 problem add /u switch – user956584 May 28 '12 at 09:09
  • 1
    for unicode there is `mb_ereg_replace` [doc](http://php.net/manual/en/function.mb-ereg-replace.php) – gondo Feb 28 '16 at 19:04
  • 1
    @cletus , great work!, with keeping this regex pattern, is there a way to get rid of all spaces at the right & and the left of the string? for example, " a b c ", would be "a b c", I know we could use trim($output), but it would be nice to have it in regex – eawedat Feb 01 '17 at 22:31
  • 1
    This does not work; did any of the nearly 400 people test this code with an input string that contained three or more consecutive spaces? ︎ – John Dec 07 '19 at 21:35
  • 1
    This should not be correct answer, as it removes all the newlines as well, question is about spaces... – prk_001 Jan 10 '20 at 11:50
  • I'm not sure what the exclamation marks do but a quick Google shows that they do something like limit the number of matches. So as a previous comment says, maybe this matches a maximum of three spaces which might negate the use of +. That said, I have just tested with three and four spaces and it works correctly for me. Can anyone explain exactly what the exclamation marks do? Are they preferable to forward slashes? – Oliver P Nov 17 '20 at 12:16
80
$output = preg_replace('/\s+/', ' ',$input);

\s is shorthand for [ \t\n\r]. Multiple spaces will be replaced with single space.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
52
preg_replace("/[[:blank:]]+/"," ",$input)
ghostdog74
  • 327,991
  • 56
  • 259
  • 343