简体   繁体   English

PHP删除仅HTML标签

[英]Php remove html only tags

It's seems quite simple - strip_tags($string); 看起来很简单strip_tags($string);

But I will repeat - html tags 但是我会重复-html标签

With use of strip tags for example 以带状标签为例

"THIS<THISTHISTHIS" >> THIS

Or 要么

$str = "You can write 'more' as < sign" >> "You can write 'more' as "

or 要么

strip_tags("you know that 5<8"); //cuts '8'

and I dont want it. 我不想要它。

I know that basicly <everything> can be tag in html, but I want to remove those default html tags 我知道基本上<everything>都可以在html中标记,但是我想删除那些默认的html标记

So why don't use put the tags you want to keep in a string and call strip_tags 那么,为什么不使用将要保留的标签放在字符串中并调用strip_tags

$keep = '<a><div><span>';
$new_html = strip_tags($html, $keep);

You can use strip_tags() with a whitelist: 您可以将strip_tags()与白名单一起使用:

strip_tags($str,'<code><em><p>');

Alternatively, you could use an HTML parser like this one for removing the HTML tags. 或者,你可以使用HTML解析器像这样一个去除HTML标签。

Hope this helps! 希望这可以帮助!

You could specify which tags you will allow in the second argument of strip_tags: 您可以在strip_tags的第二个参数中指定要允许的标签:

string strip_tags ( string $str [, string $allowable_tags ] ) 字符串strip_tags(字符串$ str [,字符串$ allowable_tags])

Check HTMLPurifier: http://htmlpurifier.org/ 检查HTMLPurifier: http : //htmlpurifier.org/

A bit hard in customization, but it gives very good filtering of html and css. 定制有点困难,但是它可以很好地过滤html和CSS。

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

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