Reputation: 33946
How do I turn $string
:
"one two three"
into:
"one two three"
?
Upvotes: 3
Views: 257
Reputation: 454960
$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.
Upvotes: 3