myTIME
myTIME

Reputation: 31

Regular expressions to remove space and whitespace in PHP?

I'm looking for regular expressions to remove space and whitespace before and after a comma.

Upvotes: 1

Views: 563

Answers (3)

Hammerite
Hammerite

Reputation: 22350

You don't need regex for this.

$output = explode(',', $input);
$output = array_map('trim', $output);
$output = implode(',', $output);

Upvotes: 1

theraccoonbear
theraccoonbear

Reputation: 4337

preg_replace('/\s*,\s*/', ',', $target_string);

Upvotes: 3

Gumbo
Gumbo

Reputation: 655795

Try this:

$output = preg_replace('/\s*,\s*/', ',', $str);

This will replace all commas with possible leading and trailing whitespace characters (\s) by a single comma.

Upvotes: 7

Related Questions