0

How do I replace with text:

<img src="anyurl">

with:

<some extra html><img src="anyurl" alt=""></someextrahtml>

What is right regex to handle <img src="*">?

HamZa
  • 14,671
  • 11
  • 54
  • 75
Jasper
  • 5,090
  • 11
  • 34
  • 41

2 Answers2

2

try this /(<img.+src="[^"]"[^>]>)/si but regex is not the preferred way to handle this. You should use DomDocument

Rob
  • 12,659
  • 4
  • 39
  • 56
1

Try this regex:

/<img\s+src="(.*?)"\s+\/?>/

It'll grap the source of images.

EDIT

Here it is in PHP!

preg_match('/<img\s+src="(.*?)"\s+\/?>/', $target, $matches);
var_dump($matches);

So your src should be in $matches[0];

HamZa
  • 14,671
  • 11
  • 54
  • 75
Paul Bain
  • 4,364
  • 1
  • 16
  • 30