2011-04-20 64 views
2

我已经使用file_get_contents()基本上获取站点的源代码到单个字符串变量。提取数据PHP字符串

源包含许多行,看起来像这样: <td align="center"><a href="somewebsite.com/something">12345</a></td>

(和很多行,不看这样的)。我想提取所有的idnumbers(12345以上)并将它们放入一个数组中。我怎样才能做到这一点?我假设我想使用某种正则表达式,然后使用preg_match_all()函数,但我不知道如何...

+1

我们必须看到的数据 – Galen 2011-04-20 19:44:06

+0

哦,太好了谷歌,而不是另外一个。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454 – Zirak 2011-04-20 19:45:27

回答

1

试试这个:

preg_match('/>[0-9]+<\/a><\/td>/', $str, $matches); 
for($i = 0;$i<sizeof($matches);$i++) 
$values[] = $matches[$i]; 
+0

谢谢!这给了我一个基本的想法,我去 preg_match_all('/ [0-9] + <\/a><\/td> /',$ html,$ matches); return $ matches [0]; 作品perfetly! – faximan 2011-04-20 20:16:08

+0

很高兴帮助:)。 – SIFE 2011-04-20 20:28:41

4

不要混淆正则表达式。获取变量并让DOM库为您完成平凡的任务。看看:http://sourceforge.net/projects/simplehtmldom/

然后你可以像树一样遍历你的HTMl并提取东西。如果你真的想得到时髦,请阅读xPath。