在 php 中的 json 字符串中用单引号替换双引号
Replace double quotes with single quotes in json string in php
我有一个 json 字符串,其中包含一些 html 及其属性。我试图在这个字符串中用单引号转义或替换双引号。我的代码适用于一些 html 属性,但不适用于所有属性。
我的例子:
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^:,{])"([^:,}])/', "".'\''."",$json);
echo htmlspecialchars($json);
//ouput: {"en":"<b class='test' size='5" >Description</b>"}
所需结果:
{"en":"<b class='test' size='5' >Description</b>"}
试试这个:str_replace('"', "'",$json);
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=str_replace('"', "'",$json);
echo htmlspecialchars($json);
输出将:{'en':'<b class='test' size='5' >Description</b>'}
我希望这能按预期工作([^{,:])"(?![},:])
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^{,:])"(?![},:])/', "".'\''."",$json);
结果
{"en":"<b class='test' size='5' >Description</b>"}
我有一个 json 字符串,其中包含一些 html 及其属性。我试图在这个字符串中用单引号转义或替换双引号。我的代码适用于一些 html 属性,但不适用于所有属性。
我的例子:
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^:,{])"([^:,}])/', "".'\''."",$json);
echo htmlspecialchars($json);
//ouput: {"en":"<b class='test' size='5" >Description</b>"}
所需结果:
{"en":"<b class='test' size='5' >Description</b>"}
试试这个:str_replace('"', "'",$json);
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=str_replace('"', "'",$json);
echo htmlspecialchars($json);
输出将:{'en':'<b class='test' size='5' >Description</b>'}
我希望这能按预期工作([^{,:])"(?![},:])
$json='{"en":"<b class="test" size="5" >Description</b>"}';
$json=preg_replace('/([^{,:])"(?![},:])/', "".'\''."",$json);
结果
{"en":"<b class='test' size='5' >Description</b>"}