Home » PHP » PHP_Compat » Bug #5106
inet_ntop function
Details
| Request #5106 | inet_ntop function |
|---|---|
| Submitted | 2005-08-16 15:00 UTC |
| From | magicaltux at gmail dot com |
| Assigned | aidan |
| Status | Closed |
| Package | PHP_Compat |
| PHP Version | Irrelevant |
| OS | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2005-08-16 15:00 UTC] magicaltux at gmail dot com
Description:
------------
Here's the inet_ntop function, which should work with old versions of PHP...
NB: inet_ntop is only available in PHP 5 >= 5.1.0 (not available yet in a stable PHP)
Requires: PHP 4.0.0 (because of foreach, could be replaced with a while(list(...)=each(...))).
This was initially a contribution I did on the PHP manual.
http://www.php.net/inet_ntop
Test script:
---------------
if (!function_exists(inet_ntop)) {
function inet_ntop($ip) {
if (strlen($ip)==4) {
// ipv4
list(,$ip)=unpack('N',$ip);
$ip=long2ip($ip);
} elseif(strlen($ip)==16) {
// ipv6
$ip=bin2hex($ip);
$ip=substr(chunk_split($ip,4,':'),0,-1);
$ip=explode(':',$ip);
$res='';
foreach($ip as $seg) {
while($seg{0}=='0') $seg=substr($seg,1);
if ($seg!='') {
$res.=($res==''?'':':').$seg;
} else {
if (strpos($res,'::')===false) {
if (substr($res,-1)==':') continue;
$res.=':';
continue;
}
$res.=($res==''?'':':').'0';
}
}
$ip=$res;
}
return $ip;
}
}