user3701384 user3701384 - 2 months ago 11
PHP Question

how to push dom html return into array in php?

This scrape returns:


  • line 1:date

  • line 2:home team

  • line 3:score

  • line 4:away team (same 4 elements repeated until the end)



I have tried everything to convert to an array, however no additions to this code have got the desired result, which would be something like a loop to give:

[1]date [2]home [3]score [4]away
etc.

until the end of doc.

<?php
$html = file_get_contents('http://www.soccerstats.com/round_details.asp?league=brazil'); //get the html returned from the following url

$doc = new DOMDocument();

libxml_use_internal_errors(TRUE);

if(!empty($html)){

$doc->loadHTML($html);
libxml_clear_errors(); //remove errors (html)

$xpath = new DOMXPath($doc);

$rows = $xpath->query('//b/font');

if($rows->length > 0){
foreach($rows as $row){
// $array[] = $row->nodeValue . "<br/>";
$array = $row->nodeValue . "<br/>";


print_r ($array);
}
}
}

?>


results:

1 Jun 14
 FluminenseÂ
1 - 1
 InternacionalÂ
1 Jun 14
 VitóriaÂ
0 - 1
 Sport RecifeÂ
1 Jun 14
 CorinthiansÂ
1 - 1
 BotafogoÂ
1 Jun 14
 ChapecoenseÂ
2 - 1
 BahiaÂ
1 Jun 14
 CruzeiroÂ
3 - 0
 FlamengoÂ
1 Jun 14
 SantosÂ
2 - 0
 CriciúmaÂ
1 Jun 14
 GrêmioÂ
0 - 0
 PalmeirasÂ
1 Jun 14
 FigueirenseÂ
1 - 3
 Atlético PRÂ
31 May 14
 São PauloÂ
2 - 1
 Atlético MGÂ
31 May 14
 CoritibaÂ
3 - 0
 GoiásÂ
30 May 14
 BahiaÂ
0 - 2
 SantosÂ
29 May 14
 InternacionalÂ
2 - 0
 ChapecoenseÂ
29 May 14
 FlamengoÂ
1 - 1
 FigueirenseÂ
29 May 14
 Atlético MGÂ
2 - 0
 FluminenseÂ
29 May 14
 Atlético PRÂ
2 - 2
 São PauloÂ
29 May 14
 CorinthiansÂ
1 - 0
 CruzeiroÂ
29 May 14
 GoiásÂ
0 - 0
 VitóriaÂ

Answer

Actually, you are already on the right path, you need to separate first the values on the tables, then from there you can use getElementsByTagName to reach for your desired values.

Consider this example: Sample Fiddle

$data = array();
$html = file_get_contents('http://www.soccerstats.com/round_details.asp?league=brazil'); //get the html returned from the following url
$doc = new DOMDocument();
libxml_use_internal_errors(true);

if(!empty($html)){
    $doc->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$html);
    libxml_clear_errors();
    $xpath = new DOMXPath($doc);

    $entries = $xpath->query('//table[@class="stat"]');
    foreach($entries as $key => $value) {

        $data[] = array(
            'date' => trim($value->getElementsByTagName('font')->item(0)->nodeValue),
            'home' => trim($value->getElementsByTagName('font')->item(1)->nodeValue),
            'score' => trim($value->getElementsByTagName('font')->item(2)->nodeValue),
            'away' => trim($value->getElementsByTagName('font')->item(3)->nodeValue),
        );
    }
}

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

Sample Fiddle

Comments