0

Let's say we have a string: <img src="/a.jpg"> - how to extract the /a.jpg?

That one is certainly the wrong one: #<img src="(.*)[^"]">#

ZombieDragon
  • 131
  • 1
  • 2
  • 12
  • 1
    possible duplicate of [Regular expression for grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/regular-expression-for-grabbing-the-href-attribute-of-an-a-element) – Gordon Jan 20 '11 at 22:03
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Jan 20 '11 at 22:08

6 Answers6

1

This should work

/<img\s+src="(.*?)"/
Damp
  • 3,328
  • 20
  • 19
0

This works (try testing here http://www.spaweditor.com/scripts/regex/index.php)

/".*[^"]/

Parris Varney
  • 11,320
  • 12
  • 47
  • 76
0

/<img src="([^"]*)">/ works.

Daniel Stelter
  • 466
  • 3
  • 6
0

May be you think in right direction with ^":

<img +src="([^"]+)">
MDI
  • 177
  • 10
0
#<img src="(.*)[^"]">#   // original

This was almost on the right track. However it combined two divergent approaches.

The (.*) is too greedy. You'll need to add a ? to make it stop early. (.*?) else it munches up quotes.

And the [^"] was redundant then. You could however use it as alternative to the unspecific .*. Just embed it in braces instead. It has the advantage of never accidentially matching the enclosing quotes. You could do the same in place of the space to make the whole regex more resilient, and leave out the closing > so it also works when extra attributes are present:

#<img[^>]+src="([^"]*)"#

This is one of the few cases where regular expressions are acceptable to use. If you want to match more complex HTML, then prefer a proper parser (phpQuery or QueryPath simplify it tremendously.)

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
0

the other answers will only work if src is the only attribute an img tag will have and it is always first.

if (preg_match('/<img\s+(?:\w+="[^"]+"\s*)*?src="([^"]+)/', $input, $matches))
  $src = $matches[1];
Walf
  • 8,535
  • 2
  • 44
  • 59