Jump to content

Manipulate Links From Dom


oauzor

Recommended Posts

Hello. This is my first post, and i hope i post this topic in the right area.

 

So, i have a script which gets content from another site using $dom and i want to manipulate the exported links.

 

I have in the script the line " $td->getElementsByTagName('a')->item(0)->setAttribute('href', 'http://google.com'); " which edit the links from col 2, and i want to make the same with the links from col 10. And in col 10 i have some problems showing the results just like on the website. Please help.

 

Thank you !

 

<?php

$content = file_get_contents("http://www.soccerway.com/national/germany/bundesliga/20122013/regular-season/");


$content = '<?xml encoding="UTF-8">' . $content;


libxml_use_internal_errors(true);


$dom = new DomDocument();
$dom->loadHTML($content);
libxml_clear_errors();


$xpath = new DomXPath($dom);
$tabele = $xpath->query('//table[contains(concat(" ", normalize-space(@class), " "), "leaguetable")]');


$tabel = $tabele->item(0);


$trs = $tabele->item(0)->getElementsByTagName('tr');


print '<table>';


foreach($trs as $rowIndex => $tr){


 $tds = $tr->getElementsByTagName('td');


 if($rowIndex > 18)
    continue;

 print '<tr>';


 foreach($tds as $colIndex => $td){



    if($colIndex == 2)
	  $td->getElementsByTagName('a')->item(0)->setAttribute('href', 'http://google.com');  


    if($colIndex > 11)
	  continue;

    // salveaza HTML
    printf('<td> %s </td>', $dom->saveXML($td));
 }

 print '</tr>';
}
print '</table>';

?>

Link to comment
https://forums.phpfreaks.com/topic/269639-manipulate-links-from-dom/
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.