note 127595 added to function.inet-pton

From: Date: Tue, 06 Sep 2022 16:16:04 +0000
Subject: note 127595 added to function.inet-pton
Groups: php.notes 
Request: Send a blank email to php-notes+get-219538@lists.php.net to get a copy of this message
class ip{ // IPV6 转数字 public static function ip_encode($ip) { $b64 = bin2hex(inet_pton($ip)); if (function_exists('gmp_init')) { return gmp_strval(gmp_init($b64,16), 10); } elseif (function_exists('bcadd')) { $num = strlen($b64); $snum = $num==8?2:4; $ipnum = 0; $ipSplit = array_map(fn($v)=>$v?base_convert($v,16,10):0, str_split($b64,$snum) ); $len = count($ipSplit)-1; foreach($ipSplit as $k=>$v){ $ipnum = bcadd($ipnum,$v?bcmul($v,$len?bcpow(16,($len)*$snum):1):0); $len--; } return $ipnum; } else { trigger_error('GMP or BCMATH extension not installed!', E_USER_ERROR); } } // 10-39位 数字转IPV6 public static function ip_decode($dec) { $snum = strlen($dec)>10?4:2; if (function_exists('gmp_init')) { $bin = gmp_strval(gmp_init($dec, 10),16); $arr = str_split($bin,$snum); if($snum==2)$arr = array_map(fn($v)=>$v?base_convert($v,16,10):0,$arr); } elseif (function_exists('bcadd')) { $arr = []; for($i=$snum*2-1;$i>-1;$i--){ $p = bcpow(16,($i)*$snum); $x = bcdiv($dec, $p, 0); $arr[] = $x; $dec = bcsub($dec,bcmul($p,$x)); } if($snum==4)$arr = array_map(fn($v)=>base_convert($v,10,16),$arr); } else { trigger_error('GMP or BCMATH extension not installed!', E_USER_ERROR); } $ip = implode(':',$arr); return $snum==2?$ip:inet_ntop(inet_pton($ip)); } } ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 240e:47d:f470:cb1f:b975:7d05:bd4b:2802) ---- Manual Page -- https://php.net/manual/en/function.inet-pton.php Edit -- https://main.php.net/note/edit/127595 Del: integrated -- https://main.php.net/note/delete/127595/integrated Del: useless -- https://main.php.net/note/delete/127595/useless Del: bad code -- https://main.php.net/note/delete/127595/bad+code Del: spam -- https://main.php.net/note/delete/127595/spam Del: non-english -- https://main.php.net/note/delete/127595/non-english Del: in docs -- https://main.php.net/note/delete/127595/in+docs Del: other reasons-- https://main.php.net/note/delete/127595 Reject -- https://main.php.net/note/reject/127595 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219538) next »