tags:

views:

23

answers:

1

I'm trying to extract src value from image tag using prag_match with perl pattern. I don't know where I'm making wrong but, I'm getting unwanted part as well. Here's how my pattern look like -

preg_match_all('#src="http.+"#',$imagetag,$temp);

$imagetag = "<img src="http://....." alt="build4.jpg" title="build4.jpg" width="320" height="240" />"

Instead of returning src="..." part, I'm getting the whole attributes - src=".." alt=".." title=".." width=".." height="..";something wrong with my patten. I would appreciate much if anyone can point out the mistake.

+3  A: 

You need to make your pattern non-greedy by using .+? in place of .+ as:

preg_match_all('#src="http.+?"#',$imagetag,$temp);
codaddict
Thanks a lot;it works. If you don't mind me asking, how does adding ? change the whole result? I mean, I thought enclosing .+ inside quotation mark would restrict return result to those with src="alphanumeric/symbol";seems to me it considers closing quotation mark of the last attribute as a match of quotation mark from my pattern and consider all the rest of attributes as a match for pattern .+.
Andrew