Regular expressions to remove space and whitespace in PHP?
I'm looking for regular expressions to remove space and whitespace 开发者_JAVA百科before and after a comma.
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.
preg_replace('/\s*,\s*/', ',', $target_string);
You don't need regex for this.
$output = explode(',', $input);
$output = array_map('trim', $output);
$output = implode(',', $output);
精彩评论