Search code examples
phphtmlcurljsdom

convert HTML table to PHP Array using JSDOM


I'd like to convert HTML this table into PHP Array

the table

What i've tried so far :

<?php

function curl($url){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch);      
    return $output;
}

$html = curl('https://www.ortax.org/ortax/?mod=kursbi');

$data = array();
foreach((@DOMDocument::loadHTML($html))->getElementsByTagName("td") as $tag){
    $data[trim($tag->textContent)]=trim($tag->nextSibling->nextSibling->textContent);
}

echo '<pre>';
print_r($data);
echo '</pre>';

?>

The result i've got : This error :

Notice: Trying to get property 'textContent' of non-object in D:\XAMPP\htdocs\curl\curl.php on line 16

and this Array :

Array
(
    [] => Error Etax-40001
    [Planning SPT Tahunan: PPh 22, 23, 25 Lebih Bayar] => 
    [Kewajiban NIK Untuk Faktur Pajak] => 
    [Billing DJBC] => 
    [Bagaimana Cara Mengatasi E-Bupot PPh 23/26 Yang Error?] => 
    [Error Etax-40001] => 
    [Dolar Australia [ AUD ]] => 1
    [1] => 0.62
    [10850.85] => 10741.47
    [10741.47] => 10796.16
    [10796.16] =>
 ) 

The result that i want, is I only want the table content in numeric array and get rid of that errors above :

Array
(
    [0] => Array
        (
            [0] => Dolar Australia [ AUD ]
            [1] => 1
            [2] => 10850.85
            [3] => 10741.47
            [4] => 10796.16
        )
)

Solution

  • It's hacky solution, but this should work.

    From the result you get it seems like you're selecting all <td> that exists in the page, you will get data you don't need. It will be better if you only select from the specific table you need, if possible.

    $table = @DOMDocument::loadHTML($html)->getElementsByTagName("table")->item(1);
    $i=-1;
    foreach($table->getElementsByTagName('tr') as $row){
        $j = 0;
        foreach($row->getElementsByTagName('td') as $tag){
            //echo $tag->textContent."<br>";
            $data[$i][$j] = $tag->textContent;
            $j++;
        }
        $i++;
    }