Regex- how to stop when the character first appears

I am trying to extract the src value from a tag, so far it seems to me that it is possible to extract a string between the src value and the final quotation mark in the string

Line

<img border="0" src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""> 

regular expression

 preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches); if($matches){ echo $matches[0]; } 

prints src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""

but I really want to print ... src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif"

or, if possible, just ... http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif

what should i add to regex? Thanks

+4
source share
3 answers

For RegExp:

 preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches); echo $matches[1]; 

If I am right, you are working with the simple_html_dom_parser library. If this is true, you can simply enter:

 $row->find('a img',0)->src 
+6
source

You were actually very close β†’

 Yours: preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches); Correct one: preg_match('/src=\"(.*?)\"/', $row->find('a img',0), $matches); 

Adding ? , you make the request for compliance .* lazy, which means that it will match anything until needed, and not something until it can. Without a lazy statement, it will stop before the last double quote " , which is located behind alt=" .

+10
source

try it should be good for your needs

 /src=\"[^\"]+\"/ 
+4
source

All Articles