简体   繁体   中英

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.

preg_replace('/\s*,\s*/', ',', $target_string);

You don't need regex for this.

$output = explode(',', $input);
$output = array_map('trim', $output);
$output = implode(',', $output);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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