3v4l.org

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

End of function normalise_ip

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.91 ms | 1415 KiB | 36 Q