2011-08-09 25 views
0

我想要獲取span id或div id內的值。例如從 <span id="lastPrice">29.00</span> 我想得到29.00。請幫忙。使用正則表達式幫助獲取跨度id中的內容

+2

使用PHP DOM,而不是正則表達式。這是不太可能打破可怕的。 – TRiG

+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – singpolyma

+0

[用PHP解析HTML的最佳方法]的可能重複(http: //stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) – Gordon

回答

0

如果你想通過regexp這樣做,那很糟糕。

$string = "<span id=\"lastPrice\">29.00</span>"; 
preg_match("/\<span id\=\"lastPrice\"\>([\d.]+?)\<\/span\>/",$string,$match); 
print "<pre>"; var_dump($match[1]); print "</pre>"; 

但還有更多更好的方法。

0

在的XPath看看=>PHP - Searching and filtering with XPath

[...] XPath是一個該死的視線比基本使用正則表達式更容易。也就是說,它可能需要一點時間才能讓你的腦袋圍繞你面臨的所有可能性!

谷歌會給你很多的例子和教程。