Regular expression help

Hey Everyone,

I am trying to figure out how to replace any images in my html code that have the src=“media.jpg”

right now this is what i have:

$data ='Some text, <img src="media.jpg"> some more text <img src="otherimg.jpg">';

$data = preg_replace('{<IMG[^>]*>(.*?)>?}', '', $data);

//i would like it to output
Some text, some more text <img src="otherimg.jpg">

But right now, it gets rid of both images

Any suggestions would be greatl appreciated, thanks!

$data = preg_replace('/<img[^>]*>/ismU', '', $data);

For more information about regular expressions in PHP, you can turn to the manual:
PCRE regex syntax
PCRE modifiers

Okay after playing around with it for a while i got it to work, I would love any feedback. Basically it removes any img tag with a defined attribute, in this case, src=“imagetoerase.gif”

function get_tag( $attr, $value, $data, $tag) {

  $tag = preg_quote($tag);

  $attr = preg_quote($attr);
  $tag_regex = "/<(".$tag.")[^>]*$attr\\\\s*=\\\\s*"."(['\\"])$value\\\\2[^>]*>/";
  $data = preg_replace($tag_regex, ' ', $data);				 

  return  $data;

$string = 'text sample text <img src="imagetoerase.gif" alt="imagetoerase.gif"/> more text more text <img src="imagetokeep.jpg" /> some more text <img src="imagetoerase.gif" alt="imagetoerase.gif"/>';

echo $string.'<br/>';
echo get_tag( 'src', 'imagetoerase.gif', $string, 'img');

Hmm, I am not sure I know how that helps me. Can you elaborate a little bit.

That seems like it does pretty much the same thing that already have, but unfortunately not the desired result…

Using regex to determine html is not the greatest idea in the world as it will lead to unreliable and unmaintainable code.

Look into something like PHP Simple HTML DOM Parser

that’s not really what i need it for, basically i have a bunch of content with a bunch of the same images that i need to get rid of.