PHP - 在引号内替换引号 - 正则表达式[关闭]

i have this text:

$var = 'bundle:a AND id:9 _query_:" bundle:b AND _query_:"bundle:c AND _query_:"bundle:d" " "';

and would like the following output:

bundle:a AND id:9 _query_:" bundle:b AND _query_:\"bundle:c AND _query_:\"bundle:d\" \""

Edit 1 :

i tried with:

preg_replace('/(?<!_:|_: )"(?=[^"]*?"(( [^:])|([,}])))/', '\\"', $var )

thanks =)

Something like that:

$var = 'bundle:a AND id:9 _query_:" bundle:b AND _query_:"bundle:c AND _query_:"bundle:d" " "';

$start = strpos($var, '"');
$end = strrpos($var, '"');

$result = substr($var, 0, $start + 1) . 
          str_replace('"', '\\"', substr($var, $start + 1, $end - $start - 1)) . 
          substr($var, $end);

Warning! I can't recommend using this code at production, just trying to show the idea.

Without Regex:

str_replace("\"", "\\\"", $var)

With Regex:

preg_replace("/\"/", "\\\"", $var)