开发者

Regular expressions to remove space and whitespace in PHP?

开发者 https://www.devze.com 2023-01-10 16:14 出处:网络
I\'m looking for regular expressions to remove space and whitespace开发者_JAVA百科before and after a comma.Try this:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号