简体   繁体   English

如何从PHP用户输入中剥离以下html标签?

[英]How to strip the following html tags from user input in PHP?

I don't want to use htmlentities() because I just want to strip 3 kinds of tags and allow all others. 我不想使用htmlentities(),因为我只想剥离3种标记并允许所有其他标记。 These are the ones to be stripped: 这些是要剥离的:

- <a hrefs...
- <scripts...
- <img src...

How can this be done? 如何才能做到这一点?

$str = strip_tags($str, '<a><script><img>');

However, if this is for security, this is not enough . 但是,如果这只是出于安全性考虑, 那还不够 Use a HTML parser and a whitelist of allowed tags and attributes. 使用HTML解析器和允许的标签和属性的白名单。

Otherwise I may enter <button onclick="alert('xss')">hello!</button> , for example. 否则,我可能会输入<button onclick="alert('xss')">hello!</button>

Check out php's strip_tags() . 查看php的strip_tags() The optional parameter allows you to select allowable tags, which I know is the opposite of what you want to, but is the better way to go. 可选参数允许您选择允许的标签,我知道这与您想要的相反,但这是更好的选择。

The other option is to use regular expressions to remove those offending tags. 另一个选择是使用正则表达式删除那些有问题的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM