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"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 25
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/pskuD
function name:  (null)
number of ops:  27
compiled vars:  !0 = $a, !1 = $string, !2 = $snmp_interface, !3 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   ASSIGN                                                   !0, <array>
  115     1      > FE_RESET_R                                       $5      !0, ->25
          2    > > FE_FETCH_R                                       ~6      $5, !1, ->25
          3    >   ASSIGN                                                   !2, ~6
  116     4        INIT_FCALL                                               'decode_snmp_interface'
          5        SEND_VAR                                                 !2
          6        DO_FCALL                                      0  $8      
          7        ASSIGN                                                   !3, $8
  117     8        FETCH_DIM_R                                      ~10     !3, 'string'
          9        IS_EQUAL                                                 !1, ~10
         10      > JMPZ                                                     ~11, ->15
  118    11    >   NOP                                                      
         12        FAST_CONCAT                                      ~12     !2, '+%09+tested+ok'
         13        ECHO                                                     ~12
  117    14      > JMP                                                      ->23
  120    15    >   ROPE_INIT                                     6  ~15     !2
         16        ROPE_ADD                                      1  ~15     ~15, '+%09+testing+failed.+must+be+'
         17        ROPE_ADD                                      2  ~15     ~15, !1
         18        ROPE_ADD                                      3  ~15     ~15, '%2C+but+is+%5B'
         19        FETCH_DIM_R                                      ~13     !3, 'string'
         20        ROPE_ADD                                      4  ~15     ~15, ~13
         21        ROPE_END                                      5  ~14     ~15, '%5D'
         22        ECHO                                                     ~14
  122    23    >   ECHO                                                     '%0A'
  115    24      > JMP                                                      ->2
         25    >   FE_FREE                                                  $5
  123    26      > RETURN                                                   1

Function hex2str:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 4
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 4
Branch analysis from position: 20
Branch analysis from position: 4
filename:       /in/pskuD
function name:  hex2str
number of ops:  22
compiled vars:  !0 = $hex, !1 = $string, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   13     1        ASSIGN                                                   !1, ''
   14     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->16
   15     4    >   INIT_FCALL                                               'chr'
          5        INIT_FCALL                                               'hexdec'
          6        FETCH_DIM_R                                      ~5      !0, !2
          7        ADD                                              ~6      !2, 1
          8        FETCH_DIM_R                                      ~7      !0, ~6
          9        CONCAT                                           ~8      ~5, ~7
         10        SEND_VAL                                                 ~8
         11        DO_ICALL                                         $9      
         12        SEND_VAR                                                 $9
         13        DO_ICALL                                         $10     
         14        ASSIGN_OP                                     8          !1, $10
   14    15        ASSIGN_OP                                     1          !2, 2
         16    >   STRLEN                                           ~13     !0
         17        SUB                                              ~14     ~13, 1
         18        IS_SMALLER                                               !2, ~14
         19      > JMPNZ                                                    ~15, ->4
   17    20    > > RETURN                                                   !1
   18    21*     > RETURN                                                   null

End of function hex2str

Function decode_snmp_interface:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 65
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 195
Branch analysis from position: 195
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 131
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 195
Branch analysis from position: 195
Branch analysis from position: 131
2 jumps found. (Code = 43) Position 1 = 135, Position 2 = 195
Branch analysis from position: 135
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 195
filename:       /in/pskuD
function name:  decode_snmp_interface
number of ops:  211
compiled vars:  !0 = $snmp_interface, !1 = $dotInterface, !2 = $b, !3 = $type, !4 = $shelf, !5 = $slot, !6 = $port, !7 = $reserved, !8 = $onu
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   38     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%28%3FP%3Cpon%3E%5Cd%2B%29%5C.%28%3FP%3Conu%3E%5Cd%2B%29%2F'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                         $9      
          6      > JMPZ                                                     $9, ->65
   53     7    >   INIT_FCALL                                               'decbin'
          8        INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 0
         11        SEND_VAL                                                 9
         12        DO_ICALL                                         $10     
         13        SEND_VAR                                                 $10
         14        DO_ICALL                                         $11     
         15        ASSIGN                                                   !2, $11
   55    16        INIT_FCALL                                               'bindec'
         17        INIT_FCALL                                               'substr'
         18        SEND_VAR                                                 !2
         19        SEND_VAL                                                 0
         20        SEND_VAL                                                 4
         21        DO_ICALL                                         $13     
         22        SEND_VAR                                                 $13
         23        DO_ICALL                                         $14     
         24        ASSIGN                                                   !3, $14
   56    25        INIT_FCALL                                               'bindec'
         26        INIT_FCALL                                               'substr'
         27        SEND_VAR                                                 !2
         28        SEND_VAL                                                 4
         29        SEND_VAL                                                 4
         30        DO_ICALL                                         $16     
         31        SEND_VAR                                                 $16
         32        DO_ICALL                                         $17     
         33        ADD                                              ~18     $17, 1
         34        ASSIGN                                                   !4, ~18
   57    35        INIT_FCALL                                               'bindec'
         36        INIT_FCALL                                               'substr'
         37        SEND_VAR                                                 !2
         38        SEND_VAL                                                 8
         39        SEND_VAL                                                 5
         40        DO_ICALL                                         $20     
         41        SEND_VAR                                                 $20
         42        DO_ICALL                                         $21     
         43        ASSIGN                                                   !5, $21
   58    44        INIT_FCALL                                               'bindec'
         45        INIT_FCALL                                               'substr'
         46        SEND_VAR                                                 !2
         47        SEND_VAL                                                 13
         48        SEND_VAL                                                 8
         49        DO_ICALL                                         $23     
         50        SEND_VAR                                                 $23
         51        DO_ICALL                                         $24     
         52        ASSIGN                                                   !6, $24
   59    53        INIT_FCALL                                               'bindec'
         54        INIT_FCALL                                               'substr'
         55        SEND_VAR                                                 !2
         56        SEND_VAL                                                 21
         57        SEND_VAL                                                 8
         58        DO_ICALL                                         $26     
         59        SEND_VAR                                                 $26
         60        DO_ICALL                                         $27     
         61        ASSIGN                                                   !7, $27
   61    62        FETCH_DIM_R                                      ~29     !1, 'onu'
         63        ASSIGN                                                   !8, ~29
   38    64      > JMP                                                      ->195
   64    65    >   CAST                                          6  ~31     !0
         66        FETCH_DIM_R                                      ~32     ~31, 0
         67        IS_EQUAL                                                 ~32, 8
         68      > JMPZ                                                     ~33, ->131
   65    69    >   INIT_FCALL                                               'decbin'
         70        SEND_VAR                                                 !0
         71        DO_ICALL                                         $34     
         72        CONCAT                                           ~35     '00', $34
         73        ASSIGN                                                   !2, ~35
   67    74        INIT_FCALL                                               'bindec'
         75        INIT_FCALL                                               'substr'
         76        SEND_VAR                                                 !2
         77        SEND_VAL                                                 0
         78        SEND_VAL                                                 4
         79        DO_ICALL                                         $37     
         80        SEND_VAR                                                 $37
         81        DO_ICALL                                         $38     
         82        ASSIGN                                                   !3, $38
   68    83        INIT_FCALL                                               'bindec'
         84        INIT_FCALL                                               'substr'
         85        SEND_VAR                                                 !2
         86        SEND_VAL                                                 4
         87        SEND_VAL                                                 4
         88        DO_ICALL                                         $40     
         89        SEND_VAR                                                 $40
         90        DO_ICALL                                         $41     
         91        ADD                                              ~42     $41, 1
         92        ASSIGN                                                   !4, ~42
   69    93        INIT_FCALL                                               'bindec'
         94        INIT_FCALL                                               'substr'
         95        SEND_VAR                                                 !2
         96        SEND_VAL                                                 8
         97        SEND_VAL                                                 5
         98        DO_ICALL                                         $44     
         99        SEND_VAR                                                 $44
        100        DO_ICALL                                         $45     
        101        ASSIGN                                                   !5, $45
   70   102        INIT_FCALL                                               'bindec'
        103        INIT_FCALL                                               'substr'
        104        SEND_VAR                                                 !2
        105        SEND_VAL                                                 13
        106        SEND_VAL                                                 3
        107        DO_ICALL                                         $47     
        108        SEND_VAR                                                 $47
        109        DO_ICALL                                         $48     
        110        ADD                                              ~49     $48, 1
        111        ASSIGN                                                   !6, ~49
   71   112        INIT_FCALL                                               'bindec'
        113        INIT_FCALL                                               'substr'
        114        SEND_VAR                                                 !2
        115        SEND_VAL                                                 16
        116        SEND_VAL                                                 8
        117        DO_ICALL                                         $51     
        118        SEND_VAR                                                 $51
        119        DO_ICALL                                         $52     
        120        ASSIGN                                                   !8, $52
   72   121        INIT_FCALL                                               'bindec'
        122        INIT_FCALL                                               'substr'
        123        SEND_VAR                                                 !2
        124        SEND_VAL                                                 24
        125        SEND_VAL                                                 8
        126        DO_ICALL                                         $54     
        127        SEND_VAR                                                 $54
        128        DO_ICALL                                         $55     
        129        ASSIGN                                                   !7, $55
   64   130      > JMP                                                      ->195
   74   131    >   CAST                                          6  ~57     !0
        132        FETCH_DIM_R                                      ~58     ~57, 0
        133        IS_EQUAL                                                 ~58, 2
        134      > JMPZ                                                     ~59, ->195
   75   135    >   INIT_FCALL                                               'decbin'
        136        SEND_VAR                                                 !0
        137        DO_ICALL                                         $60     
        138        ASSIGN                                                   !2, $60
   77   139        INIT_FCALL                                               'bindec'
        140        INIT_FCALL                                               'substr'
        141        SEND_VAR                                                 !2
        142        SEND_VAL                                                 0
        143        SEND_VAL                                                 4
        144        DO_ICALL                                         $62     
        145        SEND_VAR                                                 $62
        146        DO_ICALL                                         $63     
        147        ASSIGN                                                   !3, $63
   78   148        INIT_FCALL                                               'bindec'
        149        INIT_FCALL                                               'substr'
        150        SEND_VAR                                                 !2
        151        SEND_VAL                                                 4
        152        SEND_VAL                                                 4
        153        DO_ICALL                                         $65     
        154        SEND_VAR                                                 $65
        155        DO_ICALL                                         $66     
        156        ADD                                              ~67     $66, 1
        157        ASSIGN                                                   !4, ~67
   79   158        INIT_FCALL                                               'bindec'
        159        INIT_FCALL                                               'substr'
        160        SEND_VAR                                                 !2
        161        SEND_VAL                                                 8
        162        SEND_VAL                                                 4
        163        DO_ICALL                                         $69     
        164        SEND_VAR                                                 $69
        165        DO_ICALL                                         $70     
        166        ASSIGN                                                   !5, $70
   80   167        INIT_FCALL                                               'bindec'
        168        INIT_FCALL                                               'substr'
        169        SEND_VAR                                                 !2
        170        SEND_VAL                                                 12
        171        SEND_VAL                                                 4
        172        DO_ICALL                                         $72     
        173        SEND_VAR                                                 $72
        174        DO_ICALL                                         $73     
        175        ADD                                              ~74     $73, 1
        176        ASSIGN                                                   !6, ~74
   81   177        INIT_FCALL                                               'bindec'
        178        INIT_FCALL                                               'substr'
        179        SEND_VAR                                                 !2
        180        SEND_VAL                                                 16
        181        SEND_VAL                                                 8
        182        DO_ICALL                                         $76     
        183        SEND_VAR                                                 $76
        184        DO_ICALL                                         $77     
        185        ASSIGN                                                   !8, $77
   82   186        INIT_FCALL                                               'bindec'
        187        INIT_FCALL                                               'substr'
        188        SEND_VAR                                                 !2
        189        SEND_VAL                                                 24
        190        SEND_VAL                                                 8
        191        DO_ICALL                                         $79     
        192        SEND_VAR                                                 $79
        193        DO_ICALL                                         $80     
        194        ASSIGN                                                   !7, $80
   86   195    >   INIT_ARRAY                                       ~82     !4, 'shelf'
   87   196        ADD_ARRAY_ELEMENT                                ~82     !5, 'slot'
   88   197        ADD_ARRAY_ELEMENT                                ~82     !6, 'port'
   89   198        ADD_ARRAY_ELEMENT                                ~82     !8, 'onu'
   90   199        ROPE_INIT                                     7  ~84     !4
        200        ROPE_ADD                                      1  ~84     ~84, '%2F'
        201        ROPE_ADD                                      2  ~84     ~84, !5
        202        ROPE_ADD                                      3  ~84     ~84, '%2F'
        203        ROPE_ADD                                      4  ~84     ~84, !6
        204        ROPE_ADD                                      5  ~84     ~84, '%3A'
        205        ROPE_END                                      6  ~83     ~84, !8
        206        ADD_ARRAY_ELEMENT                                ~82     ~83, 'string'
   91   207        ADD_ARRAY_ELEMENT                                ~82     !0, 'snmp_interface'
   92   208        ADD_ARRAY_ELEMENT                                ~82     'todo', 'db_interface'
        209      > RETURN                                                   ~82
   94   210*     > RETURN                                                   null

End of function decode_snmp_interface

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.5 ms | 1034 KiB | 20 Q