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. ?>


A web developer who has a love for creativity and enjoys experimenting with various techniques in both web design and development. If you would like to be kept up to date with our posts, you can follow us on Twitter, or even by subscribing to our RSS Feed.

Leave a Reply

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

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera