2012-06-29 75 views
20

这可能看起来像一个简单的问题,但我无法在档案中找到它。reverse htmlspecialchars

如何反转htmlspecialchars的影响?

我想是这样的:

$trans_tbl = get_html_translation_table (HTML_ENTITIES); 
$trans_tbl = array_flip ($trans_tbl); 
$html = strtr ($html, $trans_tbl); 

,但没有奏效。有没有简单的方法来做到这一点?

+1

阅读本手册,它会告诉你所有与你在'see also'部分看到的功能有关的功能[这是'htmlspecialchars()']的部分(http:// php。 net/manual/en/function.htmlspecialchars.php#refsect1-function.htmlspecialchars-seealso) – Manuel

+4

这个问题似乎是无关紧要的,因为它可以在[manual](http://php.net/manual/)中找到。 EN/function.htmlspecialchars-decode.php)。 –

回答

51

使用htmlspecialchars_decode()

<?php 
$str = "<p>this -&gt; &quot;</p>\n"; 

echo htmlspecialchars_decode($str); 

// note that here the quotes aren't converted 
echo htmlspecialchars_decode($str, ENT_NOQUOTES); 
?> 

参考 - PHP Official Doc

+0

通过8秒钟,我的投票将发送给您。 –

+0

这段代码来自没有学分的官方php文档。真丢脸。 http://php.net/manual/en/function.htmlspecialchars-decode.php – Umingo

+0

@Umingo感谢您的建议,我提供了一个参考链接,但我不觉得羞耻,因为没有人头真的脱了为了这 ;) – swapnesh

4

你需要htmlspecialchars_decode()。见PHP docu on this

$html = htmlspecialchars_decode($html, ENT_NOQUOTES); 
3

例如:

echo htmlspecialchars_decode(htmlspecialchars('your "strange" text with characters like !"/$%?&*')) 

它会响应: 你的 “怪” 与字符的文本,如“/ $%&!? *

这是一个编码/解码的例子,它的工作原理是