PHP中的正则表达式从网站中提取数据

时间:2013-03-08 18:55:49

标签: php regex data-extraction

我是php的新手。作为课程作业的一部分,我需要从网站中提取数据并使用该数据呈现表格。

P.S。 :使用正则表达式不是一个好选择,但我们不允许使用DOM,jQuery等任何库。

字符集是UTF-8。

$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);

$patternform = '/<form(.*)<\/form>/sm';
preg_match_all($patternform ,$html,$matches);

这里正则表达式工作正常但是当我为表标记应用相同的正则表达式时,它返回空数组。是否与$ html中的空格有关?

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

以下代码产生了良好的结果:

$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);

$patternform = '/(<table.*<\/table>)/sm';
preg_match_all($patternform ,$html,$matches);

echo $matches[0][0];

结果:

enter image description here

相关问题