I am trying to loop through an array comprised of IP addresses collected from a text file. The ultimate goal of this is to retrieve the host name for each IP. My problem is I am only getting results from the last IP in the array. I figured it would be pretty simple, but I am clearly missing something.
<?php
$thefile = 'myfile';
$arr = file($thefile.".txt");
foreach ($arr as $data) {
echo $hostname = gethostbyaddr($data);" <br>";
?>
The text file contains several IP’s that I know have associated names, but I get
“Warning: gethostbyaddr() [function.gethostbyaddr]: Address is not a valid IPv4 or IPv6 address in…”
for every instance but the last, which returns the correct results. Thanks for any help.
You likely have a newline or some whitespace, so use trim() to remove them before passing to the function.