src/IPUtils.php
public static function sanitizeIp($ipString)
    {
        $ipString = trim($ipString);

        // CIDR notation, A.B.C.D/E
        $posSlash = strrpos($ipString, '/');
        if ($posSlash !== false) {
            $ipString = substr($ipString, 0, $posSlash);
        }

        $posColon = strrpos($ipString, ':');
        $posDot = strrpos($ipString, '.');
        if ($posColon !== false) {
            // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE
            $posRBrac = strrpos($ipString, ']');
            if ($posRBrac !== false && $ipString[0] == '[') {
                $ipString = substr($ipString, 1, $posRBrac - 1);
            }

            if ($posDot !== false) {
                // IPv4 address with port, A.B.C.D:EEEE
                if ($posColon > $posDot) {
                    $ipString = substr($ipString, 0, $posColon);
                }
                // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J
            } else if (strpos($ipString, ':') === $posColon) {
                $ipString = substr($ipString, 0, $posColon);
            }
            // else: IPv6 address, A:B:C:D:E:F:G:H
        }
        // else: IPv4 address, A.B.C.D

        return $ipString;
    }
public static function sanitizeIp($ipString)
    {
        $ipString = trim($ipString);

        // CIDR notation, A.B.C.D/E
        $posSlash = strrpos($ipString, '/');
        if ($posSlash !== false) {
            $ipString = substr($ipString, 0, $posSlash);
        }

        $posColon = strrpos($ipString, ':');
        $posDot = strrpos($ipString, '.');
        if ($posColon !== false) {
            // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE
            $posRBrac = strrpos($ipString, ']');
            if ($posRBrac !== false && $ipString[0] == '[') {
                $ipString = substr($ipString, 1, $posRBrac - 1);
            }

            if ($posDot !== false) {
                // IPv4 address with port, A.B.C.D:EEEE
                if ($posColon > $posDot) {
                    $ipString = substr($ipString, 0, $posColon);
                }
                // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J
            } else if (strpos($ipString, ':') === $posColon) {
                $ipString = substr($ipString, 0, $posColon);
            }
            // else: IPv6 address, A:B:C:D:E:F:G:H
        }
        // else: IPv4 address, A.B.C.D

        return $ipString;
    }
public static function sanitizeIp ( $ ipString ) { $ ipString = trim ( $ ipString ) ; // CIDR notation, A.B.C.D/E $ posSlash = strrpos ( $ ipString , '/' ) ; if ( $ posSlash !== false ) { $ ipString = substr ( $ ipString , 0 , $ posSlash ) ; } $ posColon = strrpos ( $ ipString , ':' ) ; $ posDot = strrpos ( $ ipString , '.' ) ; if ( $ posColon !== false ) { // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE $ posRBrac = strrpos ( $ ipString , ']' ) ; if ( $ posRBrac !== false && $ ipString [ 0 ] == '[' ) { $ ipString = substr ( $ ipString , 1 , $ posRBrac - 1 ) ; } if ( $ posDot !== false ) { // IPv4 address with port, A.B.C.D:EEEE if ( $ posColon > $ posDot ) { $ ipString = substr ( $ ipString , 0 , $ posColon ) ; } // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J } else if ( strpos ( $ ipString , ':' ) === $ posColon ) { $ ipString = substr ( $ ipString , 0 , $ posColon ) ; } // else: IPv6 address, A:B:C:D:E:F:G:H } // else: IPv4 address, A.B.C.D return $ ipString ; }
Removes the port and the last portion of a CIDR IP address.

@param string $ipString The IP address to sanitize.
@return string
Removes the port and the last portion of a CIDR IP address .