PHP ip2long() 函数将 IPv4 地址(点分 IP 地址)转换为长整数。
语法
ip2long(ip)
参数
ip | 必填。 指定标准IP地址。 |
返回值
返回长整型,如果IP地址无效则返回false .
示例:
下面的示例显示了ip2long()函数的用法。
<?php
$host="www.yxjc123.com";
$ip = gethostbyname($host);
echo "The following URLs are equivalent: \n";
echo "1. https://".$host."\n".
"2. https://".$ip."\n".
"3. https://".sprintf("%u", ip2long($ip))."/";
?>
上述代码的输出将是:
The following URLs are equivalent:
1. https://www.yxjc123.com
2. https://8.142.93.215
3. https://143547863/
示例:
考虑另一个示例,其中此函数与数字一起使用主机名。
<?php
$hosts=["www.yxjc123.com",
"www.baidu.com",
"www.youku.com",
"www.sohu.com"];
$out = "Equivalent Contents:";
foreach($hosts as $host) {
$ip = gethostbyname($host);
$out .= "\nhttps://". $host ."/ https://". $ip .
"/ https://".sprintf("%u", ip2long($ip)) ."/";
}
echo $out;
?>
上述代码的输出将是:
Equivalent Contents:
https://www.yxjc123.com/ https://8.142.93.215/ https://143547863/
https://www.baidu.com/ https://183.2.172.42/ https://3070405674/
https://www.youku.com/ https://106.11.43.215/ https://1779117015/
https://www.sohu.com/ https://58.216.6.113/ https://987235953/