简体   繁体   中英

PHP how to remove the html tags(<DIV></DIV>,<P></P>,<SPAN></SPAN>) from the string

Am using the strip_tags() to remove the HTML tags from one content . but i wants to remove the html ( <DIV></DIV>,<P></P>,<SPAN></SPAN> ) tags apart from the <img> tag and <a> , i don't want to remove the <img ><a> tag from that . but need to remove other all html tags from that content how can i do help me.

thanks

If you want to strip all tags except for <img> tag, you can do like this:

strip_tags($your_text, '<img><a>');

The second parameter of the strip_tags function allows you to specify the allowed tags. You can specify more than one tags too.

<?php
$text = '<p>Test paragraph.</p><img>sdfsdfsdfsd</img><!-- Comment --> <a href="#fragment">Other text</a>';
echo "\n";
echo strip_tags($text, '<p><a><img>');
?>

strip_tags has a lot of edge cases where it doesn't work. If you are using this on untrusted content, you'd be better off using HtmlPurifier instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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