我试图从标记中提取src值,到目前为止,我似乎能够从src值和字符串中的最终引号之间提取字符串。
字符串:
<img border="0" src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt="">,例如在PHP中:
preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
if($matches){
echo $matches[0];
}打印出 src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""
但我真正想印的是. src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif"
或者如果可能的话只是. http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif
我应该在regex中添加什么?谢谢
发布于 2012-07-02 23:49:25
对于RegExp:
preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches);
echo $matches[1];如果我是对的,您正在使用simple_html_dom_parser库。如果这是真的,你只需键入:
$row->find('a img',0)->src发布于 2012-07-03 00:08:04
你真的很亲密,>>
Yours: preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
Correct one: preg_match('/src=\"(.*?)\"/', $row->find('a img',0), $matches);通过添加?,您可以请求匹配.*惰性,这意味着它将匹配任何东西直到需要,而不是任何东西直到可以。如果没有懒惰的操作符,它将停在最后一个双引号"的前面,这是alt="的后面。
发布于 2012-07-02 23:50:51
试一试,它应该对你的需要有好处。
/src=\"[^\"]+\"/https://stackoverflow.com/questions/11302690
复制相似问题