2011-11-23 34 views
0

我想將特殊字符轉換爲HTML實體,然後再轉換回原始字符。使用php返回特殊字符的HTML實體

我已經使用htmlentities(),然後使用html_entity_decode()。它對我很好,但{}不會回到原來的字符,他們仍然{}

我現在能做什麼?

我的代碼如下所示:

$custom_header = htmlentities($custom_header); 
$custom_header = html_entity_decode($custom_header); 
+2

無法重現。 http://codepad.org/EpUQzjrx請提供一個演示此行爲的完整示例。 – deceze

+0

它適合我。 –

+0

我的$ custom_header這個樣子

<腳本類型= 「文/ JavaScript的」> $(文件)。就緒(函數(){ \t $( 「#left_side_custom_image」)。點擊(函數(){ \t \t alert(「HELLO」); \t }); });

Pritom

回答

2

即使沒有人可以複製你的問題,這裏有一個簡單的str_replace來解決它的直接方式。

$input = '<p><script type="text/javascript"> $(document).ready(function() &#123; $("#left_side_custom_image").click(function() &#123; alert("HELLO"); &#125;); &#125;); </script></p> '; 
$output = str_replace(array('&#123;', '&#125;'), array('{', '}'), $input); 

Demo(點擊 '源' 在右上方鏈接)

編輯:我現在看到的問題。如果你輸入的字符串:

"&#123;hello}" 

htmlentities調用編碼&&amp;,它給你的字符串

"&amp;#123;hello}" 

&amp;是後來解碼回&,以將其輸出:

"&#123;hello}" 

修復方法是再次發送通過html_entity_decode的字符串,這會將prop正確地解碼你的實體。

$custom_header = "&#123;hello}"; 
$custom_header = htmlentities($custom_header); 

$custom_header = html_entity_decode($custom_header); 
echo html_entity_decode($custom_header); // Outputs {hello} 
+0

感謝你爲這個答案,但如果任何其他字符包含後來哪個不是?我怎樣才能寫出這種類型的字符的完整替換代碼? – Pritom

+1

@ user1044804 - 我編輯了我的解決方案,爲您最近的粘貼添加修補程序。 – nickb

+0

非常感謝。它現在工作正常。 – Pritom