[英]Regular expressions to remove space and whitespace in PHP?
我正在寻找正则表达式以删除逗号前后的空格和空白。
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. 这将用一个逗号将所有逗号替换为可能的前导和尾随空格字符( \\s
)。
preg_replace('/\s*,\s*/', ',', $target_string);
You don't need regex for this. 您不需要正则表达式。
$output = explode(',', $input);
$output = array_map('trim', $output);
$output = implode(',', $output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.