0

loadHTMLFile用来从 HTML 页面获取表格的 td 内容,它运行良好,

define('GLPI_ROOT', '..');
$dom = new DOMDocument();
$dom->loadHTMLFile(GLPI_ROOT . "/front/yourpage.html");
$tables = $dom->getElementsByTagName('table');
$table = $tables->item(9);
$ana = $table->getElementsByTagName('td');

foreach ($ana as $td) {
  if ($td->nodeName == 'td') {

    echo $td->nodeValue,"<br/>";


  }
}

但我真正想要的只是获取td每个tr元素的第二个元素table

4

1 回答 1

0

你可以尝试这样的事情:

$cnt= 0;
foreach ($ana as $td) {
  if ($td->nodeName == 'tr')
    $cnt= 0;
  else
    $cnt++;

  if ($td->nodeName == 'td' && $cnt == 2) {

    echo $td->nodeValue,"<br/>";
  }
}
于 2013-02-23T03:03:40.560 回答