3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * ## Hex to string. For GPON SN * * @param $hex * @return string * * * @example * hex2str('5A544547')."C193E72F"; <p>return ZTEGC193E72F</p> */ function hex2str($hex) { $string = ''; for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); } return $string; } // echo hex2str('5A544547')."C193E72F"; // ZTEGC193E72F /** * ## обработка полученного от SNMP интерфейса * форматы snmp интерфейса: * - 269422592.5 * - 2432632064 * - 808398336 * @param $snmp_interface * @return array|void */ function decode_snmp_interface($snmp_interface) { // 269422592.5 if (preg_match('/(?P<pon>\d+)\.(?P<onu>\d+)/', $snmp_interface, $dotInterface)) { /** ## $dotInterface ``` Array ( [0] => 269422592.5 [pon] => 269422592 [1] => 269422592 [onu] => 5 [2] => 5 ) ``` */ $b = decbin(substr($snmp_interface, 0, 9)); $type = bindec(substr($b, 0, 4)); $shelf = bindec(substr($b, 4, 4)) + 1; $slot = bindec(substr($b, 8, 5)); $port = bindec(substr($b, 13, 8)); $reserved = bindec(substr($b, 21, 8)); $onu = $dotInterface['onu']; } // первая цифра числа == 8 elseif (strval($snmp_interface)[0] == 8) { $b = "00" . decbin($snmp_interface); $type = bindec(substr($b, 0, 4)); $shelf = bindec(substr($b, 4, 4)) + 1; $slot = bindec(substr($b, 8, 5)); $port = bindec(substr($b, 13, 3)) + 1; $onu = bindec(substr($b, 16, 8)); $reserved = bindec(substr($b, 24, 8)); } elseif (strval($snmp_interface)[0] == 2) { $b = decbin($snmp_interface); $type = bindec(substr($b, 0, 4)); $shelf = bindec(substr($b, 4, 4)) + 1; $slot = bindec(substr($b, 8, 4)); $port = bindec(substr($b, 12, 4)) + 1; $onu = bindec(substr($b, 16, 8)); $reserved = bindec(substr($b, 24, 8)); } return [ 'shelf' => $shelf, 'slot' => $slot, 'port' => $port, 'onu' => $onu, 'string' => "$shelf/$slot/$port:$onu", 'snmp_interface' => $snmp_interface, 'db_interface' => 'todo' ]; } /** * # testing */ $a = [ '269422592.5' => '1/15/16:5', '269222144.1' => '1/12/1:1', '268963840.5' => '1/8/16:5', '808398336' => '1/5/8:46', '806356736' => '1/2/1:7', '2428502272' => '1/12/1:1', '2421364992' => '1/5/4:25', '2432632064' => '1/15/16:5' ]; foreach ($a as $snmp_interface => $string){ $arr = decode_snmp_interface($snmp_interface); if($arr['string'] == $string) echo "$snmp_interface \t tested ok"; else echo "$snmp_interface \t testing failed. must be $string, but is [{$arr['string']}]"; echo "\n"; }
Output for git.master_jit, git.master, rfc.property-hooks
269422592.5 tested ok 269222144.1 tested ok 268963840.5 tested ok 808398336 tested ok 806356736 tested ok 2428502272 tested ok 2421364992 tested ok 2432632064 tested ok

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
86.73 ms | 405 KiB | 5 Q