簡體   English   中英

正則表達式 - 如何在第一次出現角色時停止

[英]regex- how to stop at first occurrence of a character

我試圖從標簽中提取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

我應該在正則表達式中添加什么? 謝謝

你實際上非常接近>>

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

添加? 你提出匹配請求.*懶惰,這意味着它將匹配任何東西,直到需要,而不是任何東西,直到可以。 沒有懶惰的運算符,它將停在最后一個雙引號" ,這是在alt="

對於RegExp:

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

如果我是對的,你正在使用simple_html_dom_parser庫。 如果這是真的你可以輸入:

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

試試,它應該對你的需求有益

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM