Skip to main content

PHP – Remove image tags from a html string with preg_replace

With websites such as a forum, users often get the feeling that they can post all kinds of images which creates headache to administrator. But you can easily remove images from a html content using PHP preg_replace and some customized regular expression code. Here is the complete code to remove image tags from a html string.

  1. <?php
  2. $html_content = ‘This is a image remove <img src=”image.png”/> example with <img src=”image.png”/> php…’;
  3. $html_content = preg_replace(‘/(<)([img])(\w+)([^>]*>)/’, , $html_content);
  4. echo $html_content;
  5. ?>

Leave a Reply

Your email address will not be published. Required fields are marked *


Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!