4

我正在尝试从标签中提取 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

我应该在正则表达式中添加什么?谢谢

4

3 回答 3

10

你其实很亲密>>

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

通过添加?你对匹配.*惰性的请求,这意味着它会匹配任何东西直到需要,而不是任何东西直到可以。如果没有惰性运算符,它将停在最后一个双引号之前",它在后面alt="

于 2012-07-03T00:08:04.800 回答
7

对于正则表达式:

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

如果我是对的,那么您正在使用simple_html_dom_parser库。如果这是真的,你可以输入:

$row->find('a img',0)->src
于 2012-07-02T23:49:25.620 回答
4

试试看,应该适合你的需求

/src=\"[^\"]+\"/
于 2012-07-02T23:50:51.640 回答