Jasper
Jasper

Reputation: 5238

Wrap <img> tag in a new tag and add alt attribute to the <img>

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="*">?

Upvotes: -1

Views: 95

Answers (2)

Paul Bain
Paul Bain

Reputation: 4397

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];

Upvotes: 1

Rob
Rob

Reputation: 12872

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

Upvotes: 2

Related Questions