WP_Http::is_ip_address()public staticWP 3.7.0

Determines if a specified string represents an IP address or not.

This function also detects the type of the IP address, returning either '4' or '6' to represent an IPv4 and IPv6 address respectively. This does not verify if the IP is a valid IP, only that it appears to be an IP address.

Метод класса: WP_Http{}

Хуков нет.

Возвращает

int|false. Upon success, '4' or '6' to represent an IPv4 or IPv6 address, false upon failure.

Использование

$result = WP_Http::is_ip_address( $maybe_ip );
$maybe_ip(строка) (обязательный)
A suspected IP address.

Список изменений

С версии 3.7.0 Введена.

Код WP_Http::is_ip_address() WP 6.4.3

public static function is_ip_address( $maybe_ip ) {
	if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $maybe_ip ) ) {
		return 4;
	}

	if ( str_contains( $maybe_ip, ':' ) && preg_match( '/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i', trim( $maybe_ip, ' []' ) ) ) {
		return 6;
	}

	return false;
}