views:

5707

answers:

5

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?

+7  A: 

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.
Sean Bright
+2  A: 

I would suggest using the strip_tags method.

Ben S
+1  A: 

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);
John Kugelman
There is no *g* flag in PHP’s PCRE implementation as replace is always performed globally.
Gumbo
@Gumbo Holy delayed update, Batman! Fixed, finally :-D
John Kugelman
A: 

Sean and John, thanks a lot!

peterk
A: 

Sean it works fine i've just used this code

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

//the result it's only the plain text. It works!!!

Yunieskid