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); var_dump($parts, $parts[$numParts - 1]); 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/l0FRZ
function name:  (null)
number of ops:  26
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     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
   41     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
   42    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
   43    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
   44    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 = 124
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 69
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 109
Branch analysis from position: 109
2 jumps found. (Code = 125) Position 1 = 110, Position 2 = 117
Branch analysis from position: 110
2 jumps found. (Code = 126) Position 1 = 111, Position 2 = 117
Branch analysis from position: 111
1 jumps found. (Code = 42) Position 1 = 110
Branch analysis from position: 110
Branch analysis from position: 117
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 117
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 89
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 109
Branch analysis from position: 109
Branch analysis from position: 89
2 jumps found. (Code = 77) Position 1 = 90, Position 2 = 108
Branch analysis from position: 90
2 jumps found. (Code = 78) Position 1 = 91, Position 2 = 108
Branch analysis from position: 91
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 107
Branch analysis from position: 94
1 jumps found. (Code = 42) Position 1 = 90
Branch analysis from position: 90
Branch analysis from position: 107
Branch analysis from position: 108
2 jumps found. (Code = 125) Position 1 = 110, Position 2 = 117
Branch analysis from position: 110
Branch analysis from position: 117
Branch analysis from position: 108
Branch analysis from position: 124
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/l0FRZ
function name:  normalise_ip
number of ops:  126
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                                                      ->125
   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, ->124
   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        INIT_FCALL                                               'var_dump'
         47        SEND_VAR                                                 !5
         48        SUB                                              ~29     !6, 1
         49        FETCH_DIM_R                                      ~30     !5, ~29
         50        SEND_VAL                                                 ~30
         51        DO_ICALL                                                 
   19    52        FETCH_DIM_R                                      ~32     !5, 0
         53        IS_IDENTICAL                                             ~32, ''
         54      > JMPZ                                                     ~33, ->69
   20    55    >   INIT_FCALL                                               'array_splice'
         56        SEND_REF                                                 !5
         57        SEND_VAL                                                 0
         58        SEND_VAL                                                 2
         59        INIT_FCALL                                               'array_fill'
         60        SEND_VAL                                                 0
         61        SUB                                              ~34     !6, 2
         62        SUB                                              ~35     8, ~34
         63        SEND_VAL                                                 ~35
         64        SEND_VAL                                                 '0000'
         65        DO_ICALL                                         $36     
         66        SEND_VAR                                                 $36
         67        DO_ICALL                                                 
         68      > JMP                                                      ->109
   21    69    >   SUB                                              ~38     !6, 1
         70        FETCH_DIM_R                                      ~39     !5, ~38
         71        IS_IDENTICAL                                             ~39, ''
         72      > JMPZ                                                     ~40, ->89
   22    73    >   ECHO                                                     'end%0A'
   23    74        INIT_FCALL                                               'array_splice'
         75        SEND_REF                                                 !5
         76        SUB                                              ~41     !6, 2
         77        SEND_VAL                                                 ~41
         78        SEND_VAL                                                 2
         79        INIT_FCALL                                               'array_fill'
         80        SEND_VAL                                                 0
         81        SUB                                              ~42     !6, 2
         82        SUB                                              ~43     8, ~42
         83        SEND_VAL                                                 ~43
         84        SEND_VAL                                                 '0000'
         85        DO_ICALL                                         $44     
         86        SEND_VAR                                                 $44
         87        DO_ICALL                                                 
         88      > JMP                                                      ->109
   25    89    > > FE_RESET_R                                       $46     !5, ->108
         90    > > FE_FETCH_R                                       ~47     $46, !7, ->108
         91    >   ASSIGN                                                   !3, ~47
   26    92        IS_IDENTICAL                                             !7, ''
         93      > JMPZ                                                     ~49, ->107
   27    94    >   INIT_FCALL                                               'array_splice'
         95        SEND_REF                                                 !5
         96        SEND_VAR                                                 !3
         97        SEND_VAL                                                 1
         98        INIT_FCALL                                               'array_fill'
         99        SEND_VAL                                                 0
        100        SUB                                              ~50     !6, 1
        101        SUB                                              ~51     8, ~50
        102        SEND_VAL                                                 ~51
        103        SEND_VAL                                                 '0000'
        104        DO_ICALL                                         $52     
        105        SEND_VAR                                                 $52
        106        DO_ICALL                                                 
   25   107    > > JMP                                                      ->90
        108    >   FE_FREE                                                  $46
   31   109    > > FE_RESET_RW                                      $54     !5, ->117
        110    > > FE_FETCH_RW                                              $54, !7, ->117
   32   111    >   INIT_FCALL                                               'sprintf'
        112        SEND_VAL                                                 '%2504s'
        113        SEND_VAR                                                 !7
        114        DO_ICALL                                         $55     
        115        ASSIGN                                                   !7, $55
   31   116      > JMP                                                      ->110
        117    >   FE_FREE                                                  $54
   34   118        INIT_FCALL                                               'implode'
        119        SEND_VAL                                                 ''
        120        SEND_VAR                                                 !5
        121        DO_ICALL                                         $57     
        122      > RETURN                                                   $57
        123*       JMP                                                      ->125
   36   124    > > RETURN                                                   <false>
   38   125*     > RETURN                                                   null

End of function normalise_ip

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.73 ms | 1411 KiB | 34 Q