note 95285 deleted from function.inet-pton by danbrown
| From: | danbrown@php.net | Date: | Wed, 23 Dec 2009 22:50:49 +0000 |
| Subject: | note 95285 deleted from function.inet-pton by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164292@lists.php.net to get a copy of this message | ||
Note Submitter: h elm u ta twa nd ls do (void) tn et
----
A little bit better function as the example below. Supports notations like ff05::1 now.
<?php
function inet_pton($ip)
{
# ipv6
if (strpos($ip, ':') !== FALSE) {
$ip = explode(':', $ip);
$res = '';
foreach ($ip as $seg) {
if ($seg === '') $res .= str_repeat('0', 4*(9-count($ip)));
else $res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
}
$ip = pack('H'.strlen($res), $res);
}
# ipv4
elseif (strpos($ip, '.') !== FALSE) {
$ip = pack('N',ip2long($ip));
}
return $ip;
}
?>