3v4l.org

run code in 300+ PHP versions simultaneously
<?php function normalise_ip($address) { if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/', $address, $matches)) { // IPv4 address $num = 0; for ($i = 1; $i <= 4; $i++) { $octet = (int) $matches[$i]; if ($octet > 255) { return false; } $num |= $octet << ((4 - $i) * 8); } return sprintf('00000000000000000000ffff%04x', $num); } else if (preg_match('/\[?((?:[0-9a-f]{1,4}:|::)+[0-9a-f]{1,4})]?/i', strtolower($address), $matches)) { // IPv6 address $parts = explode(':', $matches[1]); $numParts = count($parts); if ($parts[0] === '') { array_splice($parts, 0, 2, array_fill(0, 8 - ($numParts - 2), '0000')); } else if ($parts[$numParts - 1] === '') { echo "end\n"; array_splice($parts, $numParts - 2, 2, array_fill(0, 8 - ($numParts - 2), '0000')); } else { foreach ($parts as $i => $part) { if ($part === '') { array_splice($parts, $i, 1, array_fill(0, 8 - ($numParts - 1), '0000')); } } } foreach ($parts as &$part) { $part = sprintf('%04s', $part); } return implode('', $parts); } else { // invalid return false; } } echo normalise_ip('192.168.0.1') . "\n"; echo normalise_ip('::ffff:C0A8:0001') . "\n"; echo normalise_ip('::ffff:C0A8:1') . "\n"; echo normalise_ip('[::ffff:C0A8:0001]') . "\n"; echo normalise_ip('[C0A8:0001::]') . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EO9KL
function name:  (null)
number of ops:  26
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   INIT_FCALL                                               'normalise_ip'
          1        SEND_VAL                                                 '192.168.0.1'
          2        DO_FCALL                                      0  $0      
          3        CONCAT                                           ~1      $0, '%0A'
          4        ECHO                                                     ~1
   40     5        INIT_FCALL                                               'normalise_ip'
          6        SEND_VAL                                                 '%3A%3Affff%3AC0A8%3A0001'
          7        DO_FCALL                                      0  $2      
          8        CONCAT                                           ~3      $2, '%0A'
          9        ECHO                                                     ~3
   41    10        INIT_FCALL                                               'normalise_ip'
         11        SEND_VAL                                                 '%3A%3Affff%3AC0A8%3A1'
         12        DO_FCALL                                      0  $4      
         13        CONCAT                                           ~5      $4, '%0A'
         14        ECHO                                                     ~5
   42    15        INIT_FCALL                                               'normalise_ip'
         16        SEND_VAL                                                 '%5B%3A%3Affff%3AC0A8%3A0001%5D'
         17        DO_FCALL                                      0  $6      
         18        CONCAT                                           ~7      $6, '%0A'
         19        ECHO                                                     ~7
   43    20        INIT_FCALL                                               'normalise_ip'
         21        SEND_VAL                                                 '%5BC0A8%3A0001%3A%3A%5D'
         22        DO_FCALL                                      0  $8      
         23        CONCAT                                           ~9      $8, '%0A'
         24        ECHO                                                     ~9
         25      > RETURN                                                   1

Function normalise_ip:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 29
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
Branch analysis from position: 10
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 118
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 63
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
2 jumps found. (Code = 125) Position 1 = 104, Position 2 = 111
Branch analysis from position: 104
2 jumps found. (Code = 126) Position 1 = 105, Position 2 = 111
Branch analysis from position: 105
1 jumps found. (Code = 42) Position 1 = 104
Branch analysis from position: 104
Branch analysis from position: 111
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 111
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 83
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
Branch analysis from position: 83
2 jumps found. (Code = 77) Position 1 = 84, Position 2 = 102
Branch analysis from position: 84
2 jumps found. (Code = 78) Position 1 = 85, Position 2 = 102
Branch analysis from position: 85
2 jumps found. (Code = 43) Position 1 = 88, Position 2 = 101
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 101
Branch analysis from position: 102
2 jumps found. (Code = 125) Position 1 = 104, Position 2 = 111
Branch analysis from position: 104
Branch analysis from position: 111
Branch analysis from position: 102
Branch analysis from position: 118
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EO9KL
function name:  normalise_ip
number of ops:  120
compiled vars:  !0 = $address, !1 = $matches, !2 = $num, !3 = $i, !4 = $octet, !5 = $parts, !6 = $numParts, !7 = $part
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%2F%28%5Cd%7B1%2C3%7D%29%5C.%28%5Cd%7B1%2C3%7D%29%5C.%28%5Cd%7B1%2C3%7D%29%5C.%28%5Cd%7B1%2C3%7D%29%2F'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        DO_ICALL                                         $8      
          6      > JMPZ                                                     $8, ->29
    6     7    >   ASSIGN                                                   !2, 0
    7     8        ASSIGN                                                   !3, 1
          9      > JMP                                                      ->21
    8    10    >   FETCH_DIM_R                                      ~11     !1, !3
         11        CAST                                          4  ~12     ~11
         12        ASSIGN                                                   !4, ~12
    9    13        IS_SMALLER                                               255, !4
         14      > JMPZ                                                     ~14, ->16
   10    15    > > RETURN                                                   <false>
   12    16    >   SUB                                              ~15     4, !3
         17        MUL                                              ~16     ~15, 8
         18        SL                                               ~17     !4, ~16
         19        ASSIGN_OP                                     9          !2, ~17
    7    20        PRE_INC                                                  !3
         21    >   IS_SMALLER_OR_EQUAL                                      !3, 4
         22      > JMPNZ                                                    ~20, ->10
   14    23    >   INIT_FCALL                                               'sprintf'
         24        SEND_VAL                                                 '00000000000000000000ffff%2504x'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $21     
         27      > RETURN                                                   $21
         28*       JMP                                                      ->119
   15    29    >   INIT_FCALL                                               'preg_match'
         30        SEND_VAL                                                 '%2F%5C%5B%3F%28%28%3F%3A%5B0-9a-f%5D%7B1%2C4%7D%3A%7C%3A%3A%29%2B%5B0-9a-f%5D%7B1%2C4%7D%29%5D%3F%2Fi'
         31        INIT_FCALL                                               'strtolower'
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                         $22     
         34        SEND_VAR                                                 $22
         35        SEND_REF                                                 !1
         36        DO_ICALL                                         $23     
         37      > JMPZ                                                     $23, ->118
   16    38    >   INIT_FCALL                                               'explode'
         39        SEND_VAL                                                 '%3A'
         40        FETCH_DIM_R                                      ~24     !1, 1
         41        SEND_VAL                                                 ~24
         42        DO_ICALL                                         $25     
         43        ASSIGN                                                   !5, $25
   17    44        COUNT                                            ~27     !5
         45        ASSIGN                                                   !6, ~27
   18    46        FETCH_DIM_R                                      ~29     !5, 0
         47        IS_IDENTICAL                                             ~29, ''
         48      > JMPZ                                                     ~30, ->63
   19    49    >   INIT_FCALL                                               'array_splice'
         50        SEND_REF                                                 !5
         51        SEND_VAL                                                 0
         52        SEND_VAL                                                 2
         53        INIT_FCALL                                               'array_fill'
         54        SEND_VAL                                                 0
         55        SUB                                              ~31     !6, 2
         56        SUB                                              ~32     8, ~31
         57        SEND_VAL                                                 ~32
         58        SEND_VAL                                                 '0000'
         59        DO_ICALL                                         $33     
         60        SEND_VAR                                                 $33
         61        DO_ICALL                                                 
         62      > JMP                                                      ->103
   20    63    >   SUB                                              ~35     !6, 1
         64        FETCH_DIM_R                                      ~36     !5, ~35
         65        IS_IDENTICAL                                             ~36, ''
         66      > JMPZ                                                     ~37, ->83
   21    67    >   ECHO                                                     'end%0A'
   22    68        INIT_FCALL                                               'array_splice'
         69        SEND_REF                                                 !5
         70        SUB                                              ~38     !6, 2
         71        SEND_VAL                                                 ~38
         72        SEND_VAL                                                 2
         73        INIT_FCALL                                               'array_fill'
         74        SEND_VAL                                                 0
         75        SUB                                              ~39     !6, 2
         76        SUB                                              ~40     8, ~39
         77        SEND_VAL                                                 ~40
         78        SEND_VAL                                                 '0000'
         79        DO_ICALL                                         $41     
         80        SEND_VAR                                                 $41
         81        DO_ICALL                                                 
         82      > JMP                                                      ->103
   24    83    > > FE_RESET_R                                       $43     !5, ->102
         84    > > FE_FETCH_R                                       ~44     $43, !7, ->102
         85    >   ASSIGN                                                   !3, ~44
   25    86        IS_IDENTICAL                                             !7, ''
         87      > JMPZ                                                     ~46, ->101
   26    88    >   INIT_FCALL                                               'array_splice'
         89        SEND_REF                                                 !5
         90        SEND_VAR                                                 !3
         91        SEND_VAL                                                 1
         92        INIT_FCALL                                               'array_fill'
         93        SEND_VAL                                                 0
         94        SUB                                              ~47     !6, 1
         95        SUB                                              ~48     8, ~47
         96        SEND_VAL                                                 ~48
         97        SEND_VAL                                                 '0000'
         98        DO_ICALL                                         $49     
         99        SEND_VAR                                                 $49
        100        DO_ICALL                                                 
   24   101    > > JMP                                                      ->84
        102    >   FE_FREE                                                  $43
   30   103    > > FE_RESET_RW                                      $51     !5, ->111
        104    > > FE_FETCH_RW                                              $51, !7, ->111
   31   105    >   INIT_FCALL                                               'sprintf'
        106        SEND_VAL                                                 '%2504s'
        107        SEND_VAR                                                 !7
        108        DO_ICALL                                         $52     
        109        ASSIGN                                                   !7, $52
   30   110      > JMP                                                      ->104
        111    >   FE_FREE                                                  $51
   33   112        INIT_FCALL                                               'implode'
        113        SEND_VAL                                                 ''
        114        SEND_VAR                                                 !5
        115        DO_ICALL                                         $54     
        116      > RETURN                                                   $54
        117*       JMP                                                      ->119
   35   118    > > RETURN                                                   <false>
   37   119*     > RETURN                                                   null

End of function normalise_ip

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.17 ms | 1415 KiB | 32 Q