Subha
Subha

Reputation: 761

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

Upvotes: 0

Views: 7731

Answers (3)

troelskn
troelskn

Reputation: 117457

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.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382686

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.

Upvotes: 7

muruga
muruga

Reputation: 2122

<?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>');
?>

Upvotes: 1

Related Questions