IP.php
1<?php
2
3
4namespace Udger\Helper;
5
11class IP implements IPInterface{
12
19 public function getIpVersion($ip)
20 {
21 if (false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
22 return self::IPv6;
23 }
24
25 else if (false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
26 return self::IPv4;
27 }
28 // invalid ip
29 return false;
30 }
31
38 public function getIpLong($ip)
39 {
40 return sprintf('%u', ip2long($ip));
41 }
42
43
50 public function getIp6array($ip){
51 // expand - example: "2600:3c00::" -> "2600:3c00:0000:0000:0000:0000:0000:0000"
52 $hex = unpack("H*hex", inet_pton($ip));
53 $ipStr = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
54
55 $ipIntArray = array();
56 $ipStrArray = explode(":", $ipStr);
57
58 foreach ($ipStrArray as &$value) {
59 $ipIntArray[] = hexdec($value);
60 }
61
62 return $ipIntArray;
63 }
64}
getIpVersion($ip)
Definition: IP.php:19
getIp6array($ip)
Definition: IP.php:50
getIpLong($ip)
Definition: IP.php:38