Remove specific HTML tags using PHP

There are times when you want to remove a specific HTML tag from an HTML block of text.

This could be an anchor(<a/>>) or an image(<img/>) perhaps.

You can use preg_replace to do this quite quickly and efficiently.

Remove an anchor:

$content = "Sample text <a href='#'>Our anchor</a>. Etc etc";
$content = preg_replace('/<\/?a&#91;^>]*>/','',$content);
//$content is now -> "Sample text. Etc etc";

Remove an image:

$content = "Sample text <img src='our_image.jpg' />. Etc etc";
$content = preg_replace('/<\/?a&#91;^>]*>/','',$content);
//$content is now -> "Sample text. Etc etc";
Tags:

1 thought on “Remove specific HTML tags using PHP”

Leave a Reply

Your email address will not be published.