【问题标题】:PHP return the nth row of a html table with domPHP用dom返回html表的第n行
【发布时间】:2014-06-12 13:51:02
【问题描述】:

我正在尝试使用 simplehtmldom (http://simplehtmldom.sourceforge.net/) 打印出表格的第 n 行。目前没有任何反应,我还有什么需要做的吗?

<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.masjid-umar.org/downloads/timetable_apr.htm');
$ret = $html->find('tr', 9);
echo $ret;
?>

【问题讨论】:

  • 我能够从您的代码中获得输出而无需更改。 TUE 1
  • 谢谢。我会在另一台机器上再试一次。出于好奇,输出是以“1”结尾还是stackoverflow评论系统限制了字符?
  • 这是评论的字符数限制。

标签: php html dom html-table


【解决方案1】:

假设第 9 行是 TUE 行,您也可以使用 PHP 的内置 DOMDocument 执行此操作,这将节省一些内存和解析时间,并且不依赖于第 3 方脚本。

<?php
$html = file_get_contents('http://www.masjid-umar.org/downloads/timetable_apr.htm');
$dom = new DOMDocument();
@$dom->loadHTML($html);

//TUE 1 1 4.37 6.39 1.08 5.35 9.18 6.00 1.30 6.30 7.42 9.40                 
echo '
<table>
    <tr>';
foreach($dom->getElementsByTagName('table') as $table) {
    echo innerHTML($table->getElementsByTagName('tr')->item(9));
}
echo '
    </tr>
</table>';

function innerHTML($current){
    $ret = "";
    $nodes = @$current->childNodes;
    if(!empty($nodes)){
        foreach($nodes as $v){
            $tmp = new DOMDocument();
            $tmp->appendChild($tmp->importNode($v, true));
            $ret .= $tmp->saveHTML();
        }
        return $ret;
    }
    return;
}
?>

您还想考虑将结果缓存一天,因为网站很慢;p

【讨论】:

    【解决方案2】:

    0 是第一行,所以 8 是第九行:

    $ret = $html->find('tr', 8);
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签