htmlentities 安全地显示 html
我有来自 RSS feed 的数据。我想安全起见并使用 htmlentities,但是如果我使用它,如果其中有 html 代码,则页面充满了代码和内容。我不介意 rss 提供的格式,并且只要能安全地显示它,我就很乐意使用它。我在关注提要的内容,但也希望它的格式也正确(如果有中断标签或段落或 div)有人知道一种方法吗?
I have data that is coming in from a rss feed. I want to be safe and use htmlentities but then again if I use it if there is html code in there the page is full of code and content. I don't mind the formatting the rss offers and would be glad to use it as long as I can display it safely. I'm after the content of the feed but also want it to format decently too (if there is a break tag or paragraph or div) Anyone know a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想防止 Feed 中出现 XSS 吗?如果是这样,您需要在显示 HTML 之前运行 HTML 清理程序:
如果您只是想要转义那里的任何内容,只需调用
htmlspecialchars()
即可。但任何 HTML 都会显示为转义文本...Do you want to protect from XSS in the feed? If so, you'll need an HTML sanitizer to run on the HTML prior to displaying it:
If you just want to escape whatever is there, just call
htmlspecialchars()
on it. But any HTML will appear as escaped text...您可以使用
strip_tags
标签功能并在其中指定允许的标签:这样,允许的标签中未指定的任何标签都将被删除。
You can use the
strip_tags
tags function and specify the allowed tags in there:This way any tag not specified in allowed tags will be removed.
您可以将 HTML 转换为 mark down,然后使用各种库再次备份。
You can transform the HTML into mark down and then back up again using various libraries.