Longman\IPTools\Ip::compare PHP Method

compare() public static method

Checks if an IP is part of an IP range.
public static compare ( string $ip, string $range ) : boolean
$ip string IPv4/IPv6
$range string IP range specified in one of the following formats: Wildcard format: 1.2.3.* OR 2001:cdba:0000:0000:0000:0000:3257:* CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0 Start-End IP format: 1.2.3.0-1.2.3.255 OR 2001:cdba:0000:0000:0000:0000:3257:0001-2001:cdba:0000:0000:0000:0000:3257:1000
return boolean true if IP is part of range, otherwise false.
    public static function compare($ip, $range)
    {
        if (!self::isValid($ip)) {
            throw new \InvalidArgumentException('Input IP "' . $ip . '" is invalid!');
        }
        $status = false;
        if (strpos($range, '/') !== false) {
            $status = self::processWithSlash($range);
        } else {
            if (strpos($range, '*') !== false) {
                $status = self::processWithAsterisk($range);
            } else {
                if (strpos($range, '-') !== false) {
                    $status = self::processWithMinus($range);
                } else {
                    $status = $ip === $range;
                }
            }
        }
        return $status;
    }