2013-03-04 64 views
0

我想索引一個網站,我的preg_match返回一個空數組。使用preg_match索引

這是我到目前爲止有:

$content = get_content("www.something.com"); 
preg_match_all('#<span class="box_cod">Cod: ([0-9\.]*)</span><span class="box_pret">PRET: (.*)</span>#',$content,$Produs); 

凡get_content是捲曲功能檢索站點。

謝謝!

+2

用正則表達式解析HTML非常困難。你有沒有考慮過使用真正的DOM解析器? – 2013-03-04 12:38:17

+0

優秀...另一個機會告訴某人[託尼小馬](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )!我永遠不會厭倦這一點。 – SDC 2013-03-04 12:49:07

回答

3

您可以使用PHP Simple HTML DOM Parser解析網站內容並獲取變量。
例如你首先包括PHP文件..

// Create DOM from URL or file 
$html = file_get_html('http://www.google.com/'); 

其較容易用正則表達式解析HTML。

+0

我從來沒有使用過簡單的html dom解析器,我對它不熟悉,所以我嘗試使用正則表達式。 – 2013-03-04 12:45:01

+0

你可以查看手冊http://simplehtmldom.sourceforge.net/manual.htm – 2013-03-04 12:46:09

+0

好的,但我現在需要用preg_match_all,如果我只留下第一個使用Cod,代碼完美工作,如果我添加PRET然後它不起作用。 – 2013-03-04 12:51:51