PHP – Remove image tags from a html string with preg_replace

January 1st, 2013 by Laeeq | No comments

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

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • How much memory do PHP variables use?
  • Manipulating PHP arrays: push, pop, shift, unshift
  • 10 Super Useful PHP Code Snippets
  • Let’s play with php.ini file Configuration
  • 15+ regular expressions for PHP developers
  •  

     

    1. No comments yet.
    1. No trackbacks yet.