d.shankar Posted November 1, 2007 Share Posted November 1, 2007 I have this html source and i need to parse it with DOM .. I am halfway there but i am not able to fetch all details <table cellpadding="3" cellspacing="1" bgcolor="#CCCCCC" align="center" border="0"> <tr align="center" bgcolor="#666666"> <td><font color="#FFFFFF"><b>IP Address</b></font></td> <td><font color="#FFFFFF"><b>Country Flag</b></font></td> <td><font color="#FFFFFF"><b>County / State / Province</b></font></td> <td><font color="#FFFFFF"><b>City</b></font></td> <td><font color="#FFFFFF"><b>ISO Country Code</b></font></td> <td><font color="#FFFFFF"><b>Country</b></font></td> <td><font color="#FFFFFF"><b>ISP</b></font></td> </tr> <tr> <td bgcolor="#ffffff" align="center">61.17.176.87</td> <td bgcolor="#ffffff" align="center"><img src="images/in.gif"></td> <td bgcolor="#ffffff" align="center">TAMIL NADU</td> <td bgcolor="#ffffff" align="center">CHENNAI</td> <td bgcolor="#ffffff" align="center">IN</td> <td bgcolor="#ffffff" align="center">INDIA</td> <td bgcolor="#ffffff" align="center">VIDESH SANCHAR NIGAM LTD - INDIA</td> </tr> </table> Here is my php code $dom = new DOMDocument(); # Initializing the DOM [Document Object Model] @$dom->loadHTML($html); # Loads the source for parsing $xpath = new DOMXPath($dom); $books = $dom->getElementsByTagName( "tr" ); foreach( $books as $book ) { $authors = $book->getElementsByTagName( "td" ); $author = $authors->item(0)->nodeValue; echo htmlentities($author); echo "<br>"; } With the above code I am able to retrive only this IP Address 61.17.176.87 But i need to fetch all the remaining records. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/75611-small-php-dom-help/ Share on other sites More sharing options...
sdi126 Posted November 1, 2007 Share Posted November 1, 2007 You would need to loop through all the td tag elements inside of that array also. Quote Link to comment https://forums.phpfreaks.com/topic/75611-small-php-dom-help/#findComment-382599 Share on other sites More sharing options...
khalidorama Posted November 1, 2007 Share Posted November 1, 2007 Hi, Here is how your code should look like. <? $dom = new DOMDocument(); # Initializing the DOM [Document Object Model] $html = "the html file.html"; $dom->loadHTMLFile($html); # Loads the source for parsing $xpath = new DOMXPath($dom); $books = $dom->getElementsByTagName( "tr" ); $b = 0; $i=0; while ( $i < 7) { foreach( $books as $book ) { $authors = $book->getElementsByTagName( "td" ); $record = $authors->item(floor($b))->nodeValue; echo htmlentities($record); echo "<br>"; $b+=0.5; } $i++; echo "<BR>"; } ?> I have tried the code and it worked in my hosting server . Try if But I need to know one more thing, Does anyone of you know how to dynamically know the length of the inner array of "td" tags. In my code, as I aready know the content of html file , I put $i < 7 .. Any can help in this ? Regards Quote Link to comment https://forums.phpfreaks.com/topic/75611-small-php-dom-help/#findComment-382693 Share on other sites More sharing options...
d.shankar Posted November 1, 2007 Author Share Posted November 1, 2007 Thanks bud i will try that. Quote Link to comment https://forums.phpfreaks.com/topic/75611-small-php-dom-help/#findComment-382695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.