ip2long()函数是PHP中的内置函数,可将IPv4地址(点分IP地址)转换为长整数。
用法:
int ip2long( string $ip_address )
参数:该函数接受上述和以下描述的单个参数:
$ip_address:这是必需的参数,用于指定IP地址。 返回值:如果成功,此函数返回一个长整数,如果失败,则返回FALSE。
注意:
此函数在PHP 4.0.0和更高版本上可用。 此函数适用于不完整的IP地址。 以下示例程序旨在说明PHP中的ip2long()函数:
实例1:
<?php
// Store host name into variable
$host="geeksforgeeks.org";
// Get IP address from hostname
$ip = gethostbyname($host);
echo "These three URLs are equivalent:<br>";
$out = "1. https://" . $host . "/<br>" .
"2. https://" . $ip . "/<br>" .
"3. https://" . sprintf("%u", ip2long($ip)) . "/";
echo $out;
?>
输出:
These three URLs are equivalent:
1. https://geeksforgeeks.org/
2. https://52.25.109.230/
3. https://874081766/
实例2:
<?php
// Store the list of hostname in an arry
$hosts = array(
"geeksforgeeks.org",
"www.google.com",
"www.youtube.com",
"www.facebook.com",
"www.quora.com",
"www.stackoverflow.com"
);
$out = "Equivalent Contents:";
foreach( $hosts as $host ) {
$ip = gethostbyname($host);
$out .= "<br>https://" . $host . "/ https://" . $ip .
"/ https://" . sprintf("%u", ip2long($ip)) . "/";
}
echo $out;
?>
输出:
Equivalent Contents:
https://geeksforgeeks.org/ https://52.25.109.230/ https://874081766/
https://www.google.com/ https://172.217.167.196/ https://2899945412/
https://www.youtube.com/ https://172.217.18.206/ https://2899907278/
https://www.facebook.com/ https://185.60.216.35/ https://3107772451/
https://www.quora.com/ https://151.101.1.2/ https://2539979010/
https://www.stackoverflow.com/ https://151.101.1.69/ https://2539979077/