Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - remove <img> tag from string

Tags:

string

php

Hey, I need to delete all images from a string and I just can't find the right way to do it.

Here is what I tryed, but it doesn't work:

preg_replace("/<img[^>]+\>/i", "(image) ", $content); echo $content; 

Any ideas?

like image 408
Mike Avatar asked Jul 10 '09 00:07

Mike


2 Answers

Try dropping the \ in front of the >.

Edit: I just tested your regex and it works fine. This is what I used:

<?     $content = "this is something with an <img src=\"test.png\"/> in it.";     $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);      echo $content; ?> 

The result is:

 this is something with an (image)  in it. 
like image 115
Sean Bright Avatar answered Sep 19 '22 06:09

Sean Bright


You need to assign the result back to $content as preg_replace does not modify the original string.

$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
like image 44
John Kugelman Avatar answered Sep 19 '22 06:09

John Kugelman