0

我怎么能转换这样的东西:

"hi (text here) and (other text)" come (again)

对此:

"hi \(text here\) and \(other text\)" come (again)

基本上,我想“只”转义引号内的括号。

编辑

我是正则表达式的新手,所以我尝试了这个:

$params = preg_replace('/(\'[^\(]*)[\(]+/', '$1\\\($2', $string);

但这只会逃避第一次出现的 (.

编辑 2

也许我应该提到我的字符串可能已经转义了这些括号,在这种情况下,我不想再次转义它们。

顺便说一句,我需要它同时适用于双引号和单引号,但我认为只要我有其中一个的工作示例,我就可以做到这一点。

4

5 回答 5

1

这应该适用于单引号和双引号:

$str = '"hi \(text here)" and (other text) come \'(again)\'';

$str = preg_replace_callback('`("|\').*?\1`', function ($matches) {
    return preg_replace('`(?<!\\\)[()]`', '\\\$0', $matches[0]);
}, $str);

echo $str;

输出

"hi \(text here\)" and (other text) come '\(again\)'

它适用于 PHP >= 5.3。如果您有较低版本(> = 5),则必须将回调中的匿名函数替换为单独的函数。

于 2013-02-04T14:20:06.750 回答
1

您可以为此使用preg_replace_callback ;

// outputs: hi \(text here\) and \(other text\) come (again)
print preg_replace_callback('~"(.*?)"~', function($m) {
    return '"'. preg_replace('~([\(\)])~', '\\\$1', $m[1]) .'"';
}, '"hi (text here) and (other text)" come (again)');

已经转义的字符串呢?

// outputs: hi \(text here\) and \(other text\) come (again)
print preg_replace_callback('~"(.*?)"~', function($m) {
    return '"'. preg_replace('~(?:\\\?)([\(\)])~', '\\\$1', $m[1]) .'"';
}, '"hi \(text here\) and (other text)" come (again)');
于 2013-02-04T14:27:04.570 回答
1

给定字符串

$str = '"hi (text here) and (other text)" come (again) "maybe (to)morrow?" (yes)';

迭代法

 for ($i=$q=0,$res='' ; $i<strlen($str) ; $i++) {
   if ($str[$i] == '"') $q ^= 1;
   elseif ($q && ($str[$i]=='(' || $str[$i]==')')) $res .= '\\';
   $res .= $str[$i];
 }

 echo "$res\n";

但是如果你是递归的粉丝

 function rec($i, $n, $q) {
   global $str;
   if ($i >= $n) return '';
   $c = $str[$i];
   if ($c == '"') $q ^= 1;
   elseif ($q && ($c == '(' || $c == ')')) $c = '\\' . $c;
   return $c . rec($i+1, $n, $q);
 }

 echo rec(0, strlen($str), 0) . "\n";

结果:

"hi \(text here\) and \(other text\)" come (again) "maybe \(to\)morrow?" (yes)
于 2013-02-04T14:41:44.713 回答
1

这是使用该preg_replace_callback()功能的方法。

$str = '"hi (text here) and (other text)" come (again)';
$escaped = preg_replace_callback('~(["\']).*?\1~','normalizeParens',$str);
// my original suggestion was '~(?<=").*?(?=")~' and I had to change it
// due to your 2nd edit in your question. But there's still a chance that
// both single and double quotes might exist in your string.

function normalizeParens($m) {
    return preg_replace('~(?<!\\\)[()]~','\\\$0',$m[0]);
    // replace parens without preceding backshashes
}
var_dump($str);
var_dump($escaped);
于 2013-02-04T14:49:13.870 回答
0

这可以在没有在 regex 调用中嵌套 regex 调用的情况下完成。我也不赞同带有条件和临时变量的冗长循环。

这项任务需要召唤的英雄是\G——“继续”元字符。它允许从字符串中的某个位置开始匹配,并从最后一次匹配完成的位置继续匹配。

代码:(演示

$str = '"hi (text here) and (other text)" come (again) and "(again)", right?';

echo preg_replace(
         '~(?:\G(?!^)|"(?=[^"]+"))[^"()]*(?:"(*SKIP)(*FAIL)|\K[()])~',
         '\\\$0',
         $str
     );

输出:

"hi \(text here\) and \(other text\)" come (again) and "\(again\)", right?

细分:(演示

(?:               #start noncapturing group 1
  \G(?!^)         #continue, do not match from start of string
  |               #OR
  "(?=[^"]+")     #match double quote then lookahead for the second double quote
)                 #end noncapturing group 1
[^"()]*           #match zero or more characters not (, ), or "
(?:               #start noncapturing group 2
  "(*SKIP)(*FAIL) #consume but do not replace
  |               #OR
  \K              #forget any previously matched characters
  [()]            #match an opening or closing parenthesis
)                 #end noncapturing group 2

当转义字符应被取消其在字符串/模式中的正常含义时,此解决方案不适应边缘场景。

于 2021-09-17T10:39:35.643 回答