如何将引号(';,";)从特殊字符转换为普通字符,其他标记保持特殊


How to convert quotes(',") from special char to general with other tags remaining special?

在我的字符串中,我有引号(',")< tags >。我使用

htmlspecialchars($str, ENT_QUOTES);  

仅将单引号和双引号转换为普通文本。但不幸的是,它也在转换标签< , >。出于这个原因,即使我也有

strip_tags("$desc","< b >< font >< i >< u >< br >"); 

那些允许的标签也显示为一般的< and >符号,而不是html标签。

总之,我希望将单引号和双引号显示为常规文本,并允许标记像html一样工作。

谢谢。

为什么不在htmlspecialchar()之前运行strip_tags()?

示例:

<?php
$input = '<b>allowed tag</b> " <font>not allowed tag</font>';
// XXX
$tmp = htmlspecialchars($input, ENT_QUOTES);
$result = strip_tags($tmp, '<b>');
var_dump($result);
// string(78) "&lt;b&gt;allowed tag&lt;/b&gt; &quot; &lt;font&gt;not allowed tag&lt;/font&gt;"
// Improved
$tmp = strip_tags($input, '<b>');
$result = htmlspecialchars($tmp, ENT_QUOTES);
var_dump($result);
// string(53) "&lt;b&gt;allowed tag&lt;/b&gt; &quot; not allowed tag"