PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » PHP » PHP_Compat » Bug #5106

inet_ntop function

Details

Request #5106inet_ntop function
Submitted2005-08-16 15:00 UTC
Frommagicaltux at gmail dot com
Assignedaidan
StatusClosed
PackagePHP_Compat
PHP VersionIrrelevant
OSIrrelevant
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;
}
}