-3

i need regular expression in php to extract image src from following html code

<img class=" wp-image " width="300" height="218" alt="anything" src="image url"></img>

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91

1 Answers1

1

You should use an HTML parser for this. Nevertheless, you can do it using regex with something like this:

<img[^>\/]*src=("(?:[^\"]*)"|'(?:[^\']*)')

The image url would be in capture group 1.

Edit: If you also want to get unqouted src's you can use this:

<img[^>\/]*src=("(?:[^\"]*)"|'(?:[^\']*)'|(?:[^=<>\s\"\'`]+))
grexter89
  • 1,091
  • 10
  • 23