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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.