A
Anonymous
Guest
I'm just not on the ball this week. How'd this one slip by?
Okay, first of all, your last line with the preg_match() isn't necessary. The docs for gethostbyaddr() say that if it can't resolve a host, it'll just return the IP again. So you can just do this:
Now that you've got what we like to call a 'callback function', you can run it on every element of the array:
Like voodoo magic.
Oh, I took out that country code stuff, simply because I didn't want to deal with it. Put it back in at your leisure. I must say, I'm a little bit baffled by your logic. Why do you consider the last three parts of a user's hostname to be their ISP, or four if they have a country code? Why not the last two and three, respectively? I mean, my hostname is "12-205-143-155.client.mchsi.com". My ISP's home page is the last two parts (mchsi.com), not the last three. Anyway, I hope the above was helpful.
Okay, first of all, your last line with the preg_match() isn't necessary. The docs for gethostbyaddr() say that if it can't resolve a host, it'll just return the IP again. So you can just do this:
Code:
<?php
function get_isp($ip) {
$host = gethostbyaddr($ip);
if($host == $ip) {
$isp = '';
} else {
$host_parts = explode('.', $host);
$host_numparts = count($host_parts);
$isp = $host_parts[$host_numparts - 1] . '.' .
$host_parts[$host_numparts - 2] . '.' .
$host_parts[$host_numparts - 3];
}
return $isp;
}
?>
Now that you've got what we like to call a 'callback function', you can run it on every element of the array:
Code:
<?php
$ip_array = array('255.255.255.0', '127.0.0.1', '10.0.0.1');
// run get_isp() on every element of $ip_array
array_walk($ip_array, 'get_isp');
// print them all out, if you want:
foreach($ip_array as $isp) {
echo $isp . "<br />\n";
}
?>
Like voodoo magic.
Oh, I took out that country code stuff, simply because I didn't want to deal with it. Put it back in at your leisure. I must say, I'm a little bit baffled by your logic. Why do you consider the last three parts of a user's hostname to be their ISP, or four if they have a country code? Why not the last two and three, respectively? I mean, my hostname is "12-205-143-155.client.mchsi.com". My ISP's home page is the last two parts (mchsi.com), not the last three. Anyway, I hope the above was helpful.