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/
參考: https://www.php.net/manual/en/function.ip2long.php
相關用法
- p5.js max()用法及代碼示例
- PHP each()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP pi( )用法及代碼示例
- p5.js value()用法及代碼示例
- p5.js int()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js nf()用法及代碼示例
- p5.js day()用法及代碼示例
- CSS hsl()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- d3.js d3.min()用法及代碼示例
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | ip2long() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。