我正在努力研究正则表达式,但失败得很惨。
我有一个字符串,我想匹配并删除两个括号之间的每个空格。
例如:
This string (is an example).
会变成:
This string (isanexample).
最佳答案
您可以使用 preg_replace_callback ;
$str = "This string (is an example).";
$str = preg_replace_callback("~\(([^\)]*)\)~", function($s) {
return str_replace(" ", "", "($s[1])");
}, $str);
echo $str; // This string (isanexample).
关于php - Preg_replace 括号之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513119/