阅读网页html源代码并使用php将表添加到数组中

时间:2011-03-25 14:35:29

标签: php html parsing

我试图阅读网页的源代码并将表的信息添加到数组中

网页为http://dsrd.uc.cl/dara/libcursos/periodo21/ua5_1.html

在表格中,我想将列“profesores”添加到数组profesores []和列“nombre asignatura”到数组curso [];

我知道我可以通过以下方式获取网页来源

$homepage = file_get_contents('http://www.example.com/');

但我不知道如何管理字符串来创建数组。

1 个答案:

答案 0 :(得分:2)

一种方法是使用DOMDocument:

$doc = new DOMDocument();
$doc->loadHTML($homepage);
$table = $doc->getElementsByTagName("table")->item(/* Find out which one it is */)
$rows = $table->getElementByTagName("tr")
for ($i = 0; $i length; $i++)
{
    $row = $rows->item($i);
    /* Find columns and add it to your array */
}