IP地址子网掩码计算,返回IP段起止范围、长度、掩码等信息。
- /**
- * 掩码计算器
- * @author WeakSun <52132522@qq.com>
- * @param string $ip IP
- * @param numeric $bits 掩码
- * @return array 返回结果集
- */
- function netmask($ip, $bits = 24) {
- $result = [
- 'start' => 0, //第一个IP
- 'end' => 0, //最后一个IP
- 'length' => 0, //包含的IP个数
- 'netmask' => (~pow(2, 32 - $bits)) 1, //子网掩码
- 'networkSegment' => 0, //IP段
- 'bcast' => 0//广播地址
- ];
- $result['length'] = abs($result['netmask']) - 2;
- $result['networkSegment'] = ip2long($ip) & $result['netmask'];
- $result['start'] = $result['networkSegment'] 1;
- $result['end'] = $result['networkSegment'] $result['length'];
- $result['bcast'] = $result['end'] 1;
- return $result;
- }
复制代码
- list($ip, $bits) = explode('/', '192.168.1.5/25');
- $tmpArr = netmask($ip, $bits);
- foreach ($tmpArr as $k => &$v) {
- $k != 'length' && $v = long2ip($v);
- }
- echo '
' . var_export($tmpArr, true).' ';
复制代码
array (</li>
<li> 'start' => '192.168.1.1',</li>
<li> 'end' => '192.168.1.126',</li>
<li> 'length' => 126,</li>
<li> 'netmask' => '255.255.255.128',</li>
<li> 'networkSegment' => '192.168.1.0',</li>
<li> 'bcast' => '192.168.1.127',</li>
<li>)
复制代码
|
子網路遮罩
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31