简体   繁体   中英

Get each second 'td' element text contents , inside 'tr' elment

i'm using loadHTMLFile to get the td contents of table from an HTML page , it is working perfectly ,

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/>";


  }
}

but what i realy whant is only to get the second td element of each tr element in the table

You can try something like this:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM