Reputation: 31
I'm looking for regular expressions to remove space and whitespace before and after a comma.
Upvotes: 1
Views: 563
Reputation: 22350
You don't need regex for this.
$output = explode(',', $input);
$output = array_map('trim', $output);
$output = implode(',', $output);
Upvotes: 1
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