Jump to content

DOMDocument Code Optimization


upgrader

Recommended Posts

Hi there

 

I have wrote the code below to search the table on the URL and pick out the name value in rows 2-4, however it is not very efficient as it still does a foreach on every single row.

 

$html = file_get_contents('http://badhausen.hlstatsx.com/?mode=players&game=css'); 
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;

$tables = $dom->getElementsbyTagName('table');

$rows = $tables->item(->getElementsByTagName('tr');


$count = 1;
foreach ($rows as $row) {
if($count >= 2 && $count <=4) {
	$cols = $row->getElementsByTagName('td');
	echo "<li>".$cols->item(1)->nodeValue.'</li>';
}
$count++;
}

 

How could I optimise it?

Link to comment
https://forums.phpfreaks.com/topic/160312-domdocument-code-optimization/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.