2013-03-04 65 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