long2ip

(PHP 4, PHP 5, PHP 7, PHP 8)

long2ip Konvertiert eine Long-Integer-Adresse in einen String, der das (IPv4) Internet-Standard-Punktformat enthält ("Dotted-Format")

Beschreibung

long2ip(int $ip): string

Die Funktion long2ip() erzeugt eine IP-Adresse im Punktformat (also: www.xxx.yyy.zzz) anhand der Long-Integer-Adresse.

Parameter-Liste

ip

Eine korrekte Adressdarstellung als Long Integer.

Rückgabewerte

Gibt die IP-Adresse als String zurück.

Changelog

Version Beschreibung
8.4.0 Der Rückgabetyp wurde von string|false auf string geändert.
7.1.0 Der Parametertyp von ip wurde von string zu int geändert.

Anmerkungen

Hinweis:

Auf 32-bit-Architekturen ergibt die Umwandlung von Integer-Darstellungen von IP-Adresssen von string nach int für Zahlen, die PHP_INT_MAX überschreiten, keine richtigen Ergebnisse.

Siehe auch

  • ip2long() - Konvertiert eine gemäß IPv4-Protokoll angegebene IP-Adresse vom Punkt-Format in ein Long Integer

add a note

User Contributed Notes 2 notes

up
9
Gabriel Malca
19 years ago
If the function doesn't exist:<?    if (!function_exists("long2ip")) {        function long2ip($long) {            // Valid range: 0.0.0.0 -> 255.255.255.255            if ($long < 0 || $long > 4294967295) return false;            $ip = "";            for ($i=3;$i>=0;$i--) {                $ip .= (int)($long / pow(256,$i));                $long -= (int)($long / pow(256,$i))*pow(256,$i);                if ($i>0) $ip .= ".";            }            return $ip;        }    }?>
up
5
steve at computurn dot com
6 years ago
For a 32bit safe long2ip, which can accept string or signed integer input, try:  function safelong2ip($long) {    $binStr = sprintf("%032s", decbin((float)$long));    if (strlen($binStr) != 32) {      throw new Exception("Invalid IPv4 subnet!");    }    $ipArr = [];    for ($i = 0; $i < 4; ++$i) {      $ipArr[] = bindec(substr($binStr, $i*8, 8));    }    return implode('.', $ipArr);  }
To Top