我有一个包含一些html的json字符串,它是attrubutes。我试图在此字符串中转义或将单引号替换为双引号。我的代码可以使用某些html属性,但不能全部使用。
我的例子:
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^:,{])"([^:,}])/', "$1".'\''."$2",$json);
echo htmlspecialchars($json);
//ouput: {"en":"<b class='test' size='5" >Description</b>"}
所需结果:
{"en":"<b class='test' size='5' >Description</b>"}
我希望这能按预期工作 ([^{,:])"(?![},:])
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^{,:])"(?![},:])/', "$1".'\''."$2",$json);
结果是
{"en":"<b class='test' size='5' >Description</b>"}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句