php - 如何删除html特殊字符?

标签 php html-encode

我正在为我的应用程序创建一个 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/

相关文章:

javascript - 使用 php 在谷歌图表中获取值

php - 如何使用加入 |代码点火器 | sql|

php - 删除除有效字符以外的所有字符

asp.net - Asp.NET 5 中的 HtmlEncode 在哪里

java - 如何在 JSP 表达式语言中进行 HTML 编码?

javascript - 尝试使用正则表达式复制 SID 时获取 NULL 结果

php - 内连接中的 Doctrine 子查询

C# ASP.NET MVC 资源文件的字符串在 HTML 源中是一种奇怪的编码格式

php - 如何通过 Twig 用 html 实体转义整个 block ?

php - 在 Laravel 中触发 "select"查询返回 NULL