[PHP] Fungsi Untuk Mengambil Data Dari Table HTML ke Dalam Array

Berikut fungsi PHP untuk mengambil data dari table HTML ke dalam array 

 
function tables_to_array ($url) {
  $htmlDocDom = new DOMDocument();

  @$htmlDocDom->loadHTMLFile($url);
  $htmlDocDom->preserveWhiteSpace = false;
  $tableCounter = 0;
  $htmlDocTableArray = array();
  $htmlDocTables = $htmlDocDom->getElementsByTagName('table');
  foreach ($htmlDocTables as $htmlDocTable) {
    $htmlDocTableArray[$tableCounter] = array();
    $htmlDocRows= $htmlDocTable->getElementsByTagName('tr');
    $htmlDocRowCount = 0;
    $htmlDocTableArray[$tableCounter] = array();
    foreach ($htmlDocRows as $htmlDocRow) {
      if (strlen($htmlDocRow->nodeValue) > 1)
      {
       $htmlDocColCount = 0;
       $htmlDocTableArray[$tableCounter][$htmlDocRowCount] = array();
       $htmlDocCols = $htmlDocRow->getElementsByTagName('td');
         foreach ($htmlDocCols as $htmlDocCol) {
           $htmlDocTableArray[$tableCounter][$htmlDocRowCount][] = $htmlDocCol->nodeValue;
            $htmlDocColCount++;
        }
        $htmlDocRowCount++;
      }
    }
    if ($htmlDocRowCount > 1) $tableCounter++;
  }
  return($htmlDocTableArray);
}

Demikian, Semoga Bermanfaat…

Sumber : http://php.net/manual/en/domdocument.getelementsbytagname.php

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Blue Captcha Image
Refresh

*