lisovaccaro
lisovaccaro

Reputation: 33946

Remove extra blank spaces with PHP?

How do I turn $string:

"one    two              three"

into:

"one two three"

?

Upvotes: 3

Views: 257

Answers (2)

hungneox
hungneox

Reputation: 9829

try this:

$str = preg_replace("/[ ]{2,}/", " ", $str);

Upvotes: 0

codaddict
codaddict

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

Related Questions