我正在为我的应用程序创建一个 RSS 提要文件,我想在其中删除 HTML 标记,这是由 strip_tags
完成的。但 strip_tags
并未删除 HTML 特殊代码字符:
& ©
等等
请告诉我任何可以用来从我的字符串中删除这些特殊代码字符的函数。
最佳答案
要么使用 html_entity_decode
解码它们,要么使用 preg_replace
删除它们:
$Content = preg_replace("/&#?[a-z0-9]+;/i","",$Content);
(来自here)
编辑:根据 Jacco 的评论替代
might be nice to replace the '+' with {2,8} or something. This will limit the chance of replacing entire sentences when an unencoded '&' is present.
$Content = preg_replace("/&#?[a-z0-9]{2,8};/i","",$Content);
关于php - 如何删除html特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/657643/