Regex如何在需要时转义双引号

  • 本文关键字:转义 Regex php regex
  • 更新时间 :
  • 英文 :


我需要在响应json中将所有大整数转换为字符串。目前我的正则表达式是这样的

preg_replace(
'/:s*(-?d{16,})/',
': "$1"',
$content
);

但是这样的正则表达式的问题是,如果我的响应包含另一个json字符串,那么它里面的bigint也将被包装在字符串中,但没有转义。在这种情况下,有什么方法可以转义附加引号吗?或者用另一个正则表达式修复不正确的json ?

例子
{"example_bigint": 3330922503411457761} 
will be converted to
{"example_bigint": "3330922503411457761"} 
but
{"example_json" : "{"example_bigint": 3330922503411457761}"} 
will be converted to 
{"example_json" : "{"example_bigint": "3330922503411457761"}"} 
when expected output is
{"example_json" : "{"example_bigint": "3330922503411457761"}"} 

在json_decode函数中有一个标志:

$myJson = json_decode($jsonString, flags: JSON_BIGINT_AS_STRING);

最新更新