繁体   English   中英

如果需要,正则表达式如何转义双引号

[英]Regex how to escape double quotes if needed

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

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

但是这种正则表达式的问题是,如果我的响应包含另一个 json 字符串,那么其中的 bigints 也将被包裹在字符串中,但不会转义。 在这种情况下,有没有办法逃避附加的引号? 或者也许用另一个正则表达式修复不正确的 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM