public static function fromString($address, $mayIncludePort = true)
    {
        $result = null;
        if (is_string($address) && strpos($address, '.')) {
            $rx = '([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})';
            if ($mayIncludePort) {
                $rx .= '(?::\d+)?';
            }
            $matches = null;
            if (preg_match('/^'.$rx.'$/', $address, $matches)) {
                $ok = true;
                $nums = array();
                for ($i = 1; $ok && $i <= 4; ++$i) {
                    $ok = false;
                    $n = (int) $matches[$i];
                    if ($n >= 0 && $n <= 255) {
                        $ok = true;
                        $nums[] = (string) $n;
                    }
                }
                if ($ok) {
                    $result = new static(implode('.', $nums));
                }
            }
        }

        return $result;
    }
public static function fromString($address, $mayIncludePort = true)
    {
        $result = null;
        if (is_string($address) && strpos($address, '.')) {
            $rx = '([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})';
            if ($mayIncludePort) {
                $rx .= '(?::\d+)?';
            }
            $matches = null;
            if (preg_match('/^'.$rx.'$/', $address, $matches)) {
                $ok = true;
                $nums = array();
                for ($i = 1; $ok && $i <= 4; ++$i) {
                    $ok = false;
                    $n = (int) $matches[$i];
                    if ($n >= 0 && $n <= 255) {
                        $ok = true;
                        $nums[] = (string) $n;
                    }
                }
                if ($ok) {
                    $result = new static(implode('.', $nums));
                }
            }
        }

        return $result;
    }
public static function fromString ( $ address , $ mayIncludePort = true ) { $ result = null ; if ( is_string ( $ address ) && strpos ( $ address , '.' ) ) { $ rx = '([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})' ; if ( $ mayIncludePort ) { $ rx .= '(?::\d+)?' ; } $ matches = null ; if ( preg_match ( '/^' . $ rx . '$/' , $ address , $ matches ) ) { $ ok = true ; $ nums = array ( ) ; for ( $ i = 1 ; $ ok && $ i <= 4 ; ++ $ i ) { $ ok = false ; $ n = ( int ) $ matches [ $ i ] ; if ( $ n >= 0 && $ n <= 255 ) { $ ok = true ; $ nums [ ] = ( string ) $ n ; } } if ( $ ok ) { $ result = new static ( implode ( '.' , $ nums ) ) ; } } } return $ result ; }
Parse a string and returns an IPv4 instance if the string is valid, or null otherwise.

@param string|mixed $address the address to parse
@param bool $mayIncludePort set to false to avoid parsing addresses with ports

@return static|null
Parse a string and returns an IPv4 instance if the string is valid or null otherwise .