简体   繁体   English

正则表达式删除PHP中的空格和空格?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM