-2

So I am using the preg_match() function to extract the SRC file name of an image and that part is working well.

The problem though is it isn't stopping at the closing quotation marks of the src field. It keeps going to including "some" other stuff in the image tag but then just stops. I really just want the src URL only.

preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $rss, $image);
echo $image['src'];
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Nick
  • 1
  • 2
  • 3
    Dont use regular expression to parse HTML - http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Kostia Shiian Jan 07 '13 at 07:38

3 Answers3

0

Try out this, this may work

$regex = '/src="(.+?)"/';
then preg_match($regex,$rss,$image);
0

i am doing this with; preg_match('#src.=.".*"',$stringToMatch,$result);

CanberkP
  • 21
  • 4
0

Besides I don't advise to parse HTML like this, but this would work for you;

$str = '<img src="to/file/img.png" width="100">';
preg_match('~<img.*?src=[\'"]*(?P<src>[^\s\'"]*)~i', $str, $match);
print_r($match);
Kerem
  • 11,377
  • 5
  • 59
  • 58