3

How do I turn $string:

"one    two              three"

into:

"one two three"

?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • Duplicate of [preg_replace non-alpha, leave single whitespaces](http://stackoverflow.com/questions/8329065/preg-replace-non-alpha-leave-single-whitespaces) – Jesse Good Dec 20 '11 at 05:57

2 Answers2

3
$str = "one    two              three";
$str = preg_replace('/ +/',' ',$str);

This replaces one or more spaces with a single space. It will also replace a single space with itself!! To improve it a bit:

$str = preg_replace('/ {2,}/',' ',$str);

which replaces a group of 2 or more consecutive spaces with single space.

codaddict
  • 445,704
  • 82
  • 492
  • 529
0

try this:

$str = preg_replace("/[ ]{2,}/", " ", $str);
hungneox
  • 9,333
  • 12
  • 49
  • 66