0

I'm importing a lot of articles which contain missing images. I can match the first part of the image <img src="/display/modules/media/cropimage.php then after that it obviously differs per image.

Can I search for this entry and remove the entire image to the closing /> tag?

Thanks

Brob
  • 665
  • 2
  • 13
  • 23

3 Answers3

3

You should avoid using any regex-based solutions at all if possible. There are many ways to parse HTML with PHP that are more robust.

See this answer for an example close to what you want to do.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
2

Use preg_replace() to perform a regular expression search and replace:

$content = preg_replace("/<img src=\"/display/modules/media/cropimage.php[^>]+\>/i", "", $content); 
0b10011
  • 18,397
  • 4
  • 65
  • 86
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
1

Something like this is what you need:

$find = '/<img.*?>/i';
$replace = '';
$html = "<img src="/display/modules/media/cropimage.php alt='whatever' width='500' height='500'";
$output = preg_replace ($find, $replace, $html);
Ricardo
  • 173
  • 7
  • Your expression: `//i` is nice and simple and will work for most cases. +1 But if you want to handle those rare cases where the IMG tag has a `>` char inside a quoted attribute value (which is perfectly valid in HTML), I would recommend using this one instead: `/]|'[^']*'|"[^"]*")*>/i` – ridgerunner Jul 26 '11 at 13:13