PHP.mk документација

inet_pton

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.inet-pton.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.inet-pton.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
inet_pton

Референца за `function.inet-pton.php` со подобрена типографија и навигација.

function.inet-pton.php

inet_pton

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

inet_pton(PHP 5 >= 5.1.0, PHP 7, PHP 8)

= NULL

inet_pton(string $ip): string|false

Ги конвертира човечки читливата IP адреса во нејзината спакувана in_addr репрезентација

Параметри

ip

Оваа функција конвертира човечки читлива IPv4 или IPv6 адреса (ако PHP е компајлиран со овозможена IPv6 поддршка) во соодветна 32-битна или 128-битна бинарна структура за семејство адреси.

Вратени вредности

Низа претстава на грешката in_addr Човечки читлива IPv4 или IPv6 адреса. ip, или false репрезентација на дадената ip ако е дадена синтаксички невалидна

Примери

Пример #1 inet_pton() Пример

<?php
$in_addr
= inet_pton('127.0.0.1');

$in6_addr = inet_pton('::1');
?>

Види Исто така

  • ip2long() - Конвертира стринг што содржи (IPv4) интернет протокол адреса со точки во долг цел број
  • long2ip() на 32-битни системи поради прелевање на цел број.
  • inet_ntop() (на пример, IPv4 адреса без точки или IPv6 адреса без две точки).

Белешки од корисници 6 белешки

TuRn3r
пред 11 години
Be careful, address with leading 0 return false.

Example : 
<?php
inet_pton('172.27.1.04'); // return false
inet_pton('172.27.1.4') ;// return the good result
?>
- Конвертира спакувана интернет адреса во човечки читлива репрезентација
пред 14 години
It is possible to verify if PHP was compiled with --disable-ipv6 option by AF_INET6 constant.

<?php

if (defined('AF_INET6')) {
  echo "PHP was compiled without --disable-ipv6 option";
} else {
  echo "PHP was compiled with --disable-ipv6 option";
}

?>
francis dot besset at gmail dot com
19 години пред
If you want to use the above function you should test for ':' character before '.'. Meaning, you should check if it's an ipv6 address before checking for ipv4.
Why? IPv6 allows this type of notation:

::127.0.0.1

If you check for '.' character you will think this is an ipv4 address and it will fail.
me at diogoresende dot net
пред 5 години
Regarding the ::127.0.0.1 notation

It is a very special case that needs not to be handled. This kind of notation is reserved for ipv4-compatible ipv6 address. 
For example the notation ::ffff:192.0.2.128 can be easily read as "ipv6 address that maps to ipv4 address 192.0.2.128"

However as RFC says:
https://tools.ietf.org/html/rfc5156#page-2

2.2.  IPv4-Mapped Addresses

   ::FFFF:0:0/96 are the IPv4-mapped addresses [RFC4291].  Addresses
   within this block should not appear on the public Internet.

2.3.  IPv4-Compatible Addresses

   ::<ipv4-address>/96 are the IPv4-compatible addresses [RFC4291].
   These addresses are deprecated and should not appear on the public
   Internet.

This means that you only need to handle this kind of notation if you need to be compatible with private IP's
akashwebdev at gmail dot com
пред 16 години
If the input string is not a readable IP address, inet_pton() generates an E_WARNING and returns FALSE.  The same is true for inet_ntop().

Also, inet_pton() does not recognize netmask notation (e.g: "1.2.3.4/24" or "1:2::3:4/64") in the input string.  This differs from how some database systems (like postgreSQL) support IP address types, so if you need that sort of functionality when processing IP addresses in PHP you'll have to write it in yourself.

A rough example:

<?php

// Sample IP addresses
$ipaddr = '1.2.3.4/24'; // IPv4 with /24 netmask
$ipaddr = '1:2::3:4/64'; // IPv6 with /64 netmask

// Strip out the netmask, if there is one.
$cx = strpos($ipaddr, '/');
if ($cx)
{
  $subnet = (int)(substr($ipaddr, $cx+1));
  $ipaddr = substr($ipaddr, 0, $cx);
}
else $subnet = null; // No netmask present

// Convert address to packed format
$addr = inet_pton($ipaddr);

// Let's display it as hexadecimal format
foreach(str_split($addr) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
echo "<br />\n";

// Convert the netmask
if (is_integer($subnet))
{
  // Maximum netmask length = same as packed address
  $len = 8*strlen($addr);
  if ($subnet > $len) $subnet = $len;
  
  // Create a hex expression of the subnet mask
  $mask  = str_repeat('f', $subnet>>2);
  switch($subnet & 3)
  {
  case 3: $mask .= 'e'; break;
  case 2: $mask .= 'c'; break;
  case 1: $mask .= '8'; break;
  }
  $mask = str_pad($mask, $len>>2, '0');

  // Packed representation of netmask
  $mask = pack('H*', $mask);
}

// Display the netmask as hexadecimal
foreach(str_split($mask) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);

?>
admin at hanzlsoft dot eu
пред 11 години
If you are receiving an "Unrecognized address" error for an IPv6 address, it's possible your version of PHP has not been compiled with IPv6 support.

To check, load up phpinfo(); and look to see if "IPv6 Support" is set to "disabled".
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.