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 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
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
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
48.2 ms | 407 KiB | 5 Q