Replace all occurences of char inside quotes on PHP

女生的网名这么多〃 提交于 2019-12-02 18:02:50

问题


How could I convert something like this:

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

To this:

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

Basically, I want to escape "only" the parentheses that are inside quotes.

EDIT

I'm to new in Regex and so, I tried this:

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

But this will just escape the first occurrence of (.

EDIT 2

Maybe I should mention that my string could have these parentheses already escaped and, in this case, I don't want to escaped them again.

By the way, I need it working for both double and single quotes but I think that I can do that as long as I have a working example for one of them.


回答1:


This should do it for both single and double quotes:

$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;

output

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

It is for PHP >= 5.3. If you have a lower version (>=5) you have to replace the anonymous function in the callback with a separate function.




回答2:


You can use preg_replace_callback for this;

// 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)');

What about already escaped strings;

// 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)');



回答3:


Given the string

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

Iterative method

 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";

But if you're a fan of recursion

 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";

Result:

"hi \(text here\) and \(other text\)" come (again) "maybe \(to\)morrow?" (yes)



回答4:


Here's how you can do it with the preg_replace_callback() function.

$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);


来源:https://stackoverflow.com/questions/14688462/replace-all-occurences-of-char-inside-quotes-on-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!