2016-03-01 82 views
0

我有使用json_encode($data, JSON_HEX_QUOT | JSON_HEX_TAG)如何解碼用JSON_HEX_QUOT編碼的html字符串| JSON_HEX_TAG

&後,其通過PHP json_encode使用JSON恆定

編碼HTML之前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 
+0

向我們展示一個具體的JSON示例**,緊接在**'json_encode'和**之前**'json_decode'之前。 – deceze

+1

無法重現任何問題:https://3v4l.org/Q5NKL。 – deceze

+0

我的直覺反應是你在某個階段虐待反斜槓,並從'\ u003C'中移除''''''''。很顯然,我看不到在哪裏或如何發生...... – deceze

回答

0

當使用JSON編碼在數據屬性,範數使用JSON編碼選項,而是爲標準機制無法逃脫轉義爲HTML屬性。在PHP中,可能是諸如htmlspecialchars或htmlspecialentities和HTML上下文的適當選項。

當使用JSON編碼在JavaScript中嵌入數據時,您應該只需要JSON_HEX_TAG。

不要在此引用我,因爲可能會有情況或方面被遺漏。瀏覽器有一個習慣,即允許跨越某些限制。另一方面,我並不認爲爲了萬一,任意逃脫是合理的。

您可以通過JSON_UNESCAPED_UNICODE輸出原始UTF8而不是轉義。一些解析器反對這個。你應該檢查標準對此有何評論。