如何解码用JSON_HEX_QUOT |编码的html字符串JSON_HEX_TAG

时间:2016-03-01 10:43:47

标签: php json

我的html由php json_encode编码,json常量

使用json_encode( $data, JSON_HEX_QUOT | JSON_HEX_TAG )

之后&在json_encode / json_decode

之前
{"agent_designation":"Agent","agent_desc":"\u003Cp\u003EAhmed Khan is a commercial consultant, a B.School MBA with flair of consulting businesses. With insightful perception of end user concerns to methodical compliance in consulting Investor groups, his analytical perspective & proactive approach gave significant recognition. His transparency of information and numbers has gained him virtual clients internationally. He has an unchallenging advantage, in consulting business expansion clients conveying value from concept to completion. His consultation justifies a propertyu2019s relevance, while offering holistic solutions from cost to return on investments.\u003C\/p\u003E","agent_img":"\/wp-content\/uploads\/2015\/09\/Ahmed-Khan-31.jpg","agent_linkedin":"","agent_phone":""}

$data包含html,它可以是单引号双引号斜杠的html,有时只是一个简单的文本。

<p>Ahmed Khan is a commercial consultant, a B.School MBA with flair of consulting businesses. With insightful perception of end user concerns to methodical compliance in consulting Investor groups, his analytical perspective & proactive approach gave significant recognition. His transparency of information and numbers has gained him virtual clients internationally. He has an unchallenging advantage, in consulting business expansion clients conveying value from concept to completion. His consultation justifies a propertyu2019s relevance, while offering holistic solutions from cost to return on investments.</p>
  

在渲染此编码的html时会出现问题   (u003Cpu003E)这些类型的角色。

使用json_decode

进行渲染
        $agent_info = @json_decode( $data );
        echo '<pre>';print_r($agent_info);echo '</pre>';

输出

u003Cpu003EAhmed Khan is a commercial consultant, a B.School MBA with flair of consulting businesses. With insightful perception of end user concerns to methodical compliance in consulting Investor groups, his analytical perspective & proactive approach gave significant recognition. His transparency of information and numbers has gained him virtual clients internationally. He has an unchallenging advantage, in consulting business expansion clients conveying value from concept to completion. His consultation justifies a propertyu2019s relevance, while offering holistic solutions from cost to return on investments.u003C/pu003E

1 个答案:

答案 0 :(得分:0)

在数据属性中使用JSON编码时,规范是不使用JSON编码选项进行转义,而是转义HTML属性的标准机制。在PHP中,可能是诸如htmlspecialchars或htmlspecialentities之类的,具有适用于HTML上下文的选项。

使用JSON编码在javascript中嵌入数据时,您应该只需要JSON_HEX_TAG。

不要引用我的话,因为可能会遗漏一些情景或方面。浏览器习惯于有怪癖,允许双向某些约束。另一方面,我不认为随意逃避以防万一是合理的。

您可以传递JSON_UNESCAPED_UNICODE来输出原始UTF8而不是转义。一些解析器反对这一点。你应该检查一下标准的含义。