Jump to content

Small PHP DOM Help.


d.shankar

Recommended Posts

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.

Link to comment
Share on other sites

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

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.