1

I'm updating my website to set the rDNS, however I'm here to ask your opinion on how this handle my issue. I have all IP's (IPv4) in a array called $aIP. Now I have a list like this:

<tr><td>1.2.3.4</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=1">myserver.com</a></td></tr>
<tr><td>1.2.3.5</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=2"><i>not set</i></a></td></tr>
<tr><td>1.2.3.6</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=3"><i>not set</i></a></td></tr>
<tr><td>1.2.3.7</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=4">test.myserver.com</a></td></tr>
<tr><td>1.2.3.8</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=5"><i>not set</i></a></td></tr>
<tr><td>1.2.3.9</td><td>hostname.bla.com</td><td><a href="edit-reverse.cgi?id=6"><i>not set</i></a></td></tr>

Now I need the current rDNS value (in this case either myserver.com or not set or test.myserver.com) and I need the value or the complete url which it links to (edit-reverse.cgi?id=1 or 1) which is linked to the IP address in the array $aIP.

This would be the expected output (not especially in this output format rather be in an array or something):

1.2.3.4 => 1, myserver.com
1.2.3.5 => 2, not set
1.2.3.6 => 3, not set
1.2.3.7 => 4, test.myserver.com
1.2.3.8 => 5, not set
1.2.3.9 => 6, not set

Please bear in mind that not all IP addresses I have might be in the $aIP array, so basically it should loop through the HTML code and search for the values according to the $aIP array.

I was thinking about using regex but then I don't know a lot about them so it would probably be very inefficient code. What would be the best way to handle this?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Devator
  • 3,686
  • 4
  • 33
  • 52

1 Answers1

2

I've found a perfect solution, using native functions:

/*** a new dom object ***/ 
$dom = new domDocument; 

/*** load the html into the object ***/ 
$dom->loadHTML($html); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 

/*** get all rows from the table ***/ 
$rows = $tables->item(0)->getElementsByTagName('tr'); 

/*** loop over the table rows ***/ 
foreach ($rows as $row) 
{ 
    /*** get each column by tag name ***/ 
    $cols = $row->getElementsByTagName('td'); 
    /*** echo the values ***/ 
    echo $cols->item(0)->nodeValue.'<br />'; 
    echo $cols->item(1)->nodeValue.'<br />'; 
    echo $cols->item(2)->nodeValue; 
    echo '<hr />'; 
}

Found at http://www.phpro.org/examples/Parse-HTML-With-PHP-And-DOM.html

Thanks all.

Devator
  • 3,686
  • 4
  • 33
  • 52