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


how to decode string of html encoded with JSON_HEX_QUOT | JSON_HEX_TAG

我有由php json_encode和json常量编码的html

使用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

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

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

不要引用我的话,因为可能会遗漏一些场景或方面。浏览器习惯于有允许绕过某些约束的怪癖。另一方面,我不认为为了以防万一而任意逃跑是合理的。

您可以传递JSON_UNESCAPED_UNICODE以输出原始 UTF8 而不是转义。不过,一些解析器反对这一点。您应该检查标准对此有何说明。