3v4l.org

run code in 300+ PHP versions simultaneously
<?php function normalise_ip($address) { $address = trim(strtolower($address)); if (preg_match('/^\[?(?:(?:(?:0{0,4}:)*|:):ffff:)?(\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); if ($parts[0] === '') { array_splice($parts, 0, 2, array_fill(0, 8 - ($numParts - 2), '0000')); } else if ($parts[$numParts - 1] === '') { 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('[::ffff:192.168.0.1]') . "\n"; echo normalise_ip('[0:0:0:0:0:ffff:192.168.0.1]') . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ip8dg
function name:  (null)
number of ops:  31
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                                                 '%5B%3A%3Affff%3A192.168.0.1%5D'
         22        DO_FCALL                                      0  $8      
         23        CONCAT                                           ~9      $8, '%0A'
         24        ECHO                                                     ~9
   45    25        INIT_FCALL                                               'normalise_ip'
         26        SEND_VAL                                                 '%5B0%3A0%3A0%3A0%3A0%3Affff%3A192.168.0.1%5D'
         27        DO_FCALL                                      0  $10     
         28        CONCAT                                           ~11     $10, '%0A'
         29        ECHO                                                     ~11
         30      > 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 = 121
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 67
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 106
Branch analysis from position: 106
2 jumps found. (Code = 125) Position 1 = 107, Position 2 = 114
Branch analysis from position: 107
2 jumps found. (Code = 126) Position 1 = 108, Position 2 = 114
Branch analysis from position: 108
1 jumps found. (Code = 42) Position 1 = 107
Branch analysis from position: 107
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 114
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 71, Position 2 = 86
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 106
Branch analysis from position: 106
Branch analysis from position: 86
2 jumps found. (Code = 77) Position 1 = 87, Position 2 = 105
Branch analysis from position: 87
2 jumps found. (Code = 78) Position 1 = 88, Position 2 = 105
Branch analysis from position: 88
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 104
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
Branch analysis from position: 104
Branch analysis from position: 105
2 jumps found. (Code = 125) Position 1 = 107, Position 2 = 114
Branch analysis from position: 107
Branch analysis from position: 114
Branch analysis from position: 105
Branch analysis from position: 121
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ip8dg
function name:  normalise_ip
number of ops:  123
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%5C%5B%3F%28%3F%3A%28%3F%3A%28%3F%3A0%7B0%2C4%7D%3A%29%2A%7C%3A%29%3Affff%3A%29%3F%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%5D%3F%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                                                      ->122
   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, ->121
   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        FETCH_DIM_R                                      ~31     !5, 0
         51        IS_IDENTICAL                                             ~31, ''
         52      > JMPZ                                                     ~32, ->67
   21    53    >   INIT_FCALL                                               'array_splice'
         54        SEND_REF                                                 !5
         55        SEND_VAL                                                 0
         56        SEND_VAL                                                 2
         57        INIT_FCALL                                               'array_fill'
         58        SEND_VAL                                                 0
         59        SUB                                              ~33     !6, 2
         60        SUB                                              ~34     8, ~33
         61        SEND_VAL                                                 ~34
         62        SEND_VAL                                                 '0000'
         63        DO_ICALL                                         $35     
         64        SEND_VAR                                                 $35
         65        DO_ICALL                                                 
         66      > JMP                                                      ->106
   22    67    >   SUB                                              ~37     !6, 1
         68        FETCH_DIM_R                                      ~38     !5, ~37
         69        IS_IDENTICAL                                             ~38, ''
         70      > JMPZ                                                     ~39, ->86
   23    71    >   INIT_FCALL                                               'array_splice'
         72        SEND_REF                                                 !5
         73        SUB                                              ~40     !6, 2
         74        SEND_VAL                                                 ~40
         75        SEND_VAL                                                 2
         76        INIT_FCALL                                               'array_fill'
         77        SEND_VAL                                                 0
         78        SUB                                              ~41     !6, 2
         79        SUB                                              ~42     8, ~41
         80        SEND_VAL                                                 ~42
         81        SEND_VAL                                                 '0000'
         82        DO_ICALL                                         $43     
         83        SEND_VAR                                                 $43
         84        DO_ICALL                                                 
         85      > JMP                                                      ->106
   25    86    > > FE_RESET_R                                       $45     !5, ->105
         87    > > FE_FETCH_R                                       ~46     $45, !7, ->105
         88    >   ASSIGN                                                   !3, ~46
   26    89        IS_IDENTICAL                                             !7, ''
         90      > JMPZ                                                     ~48, ->104
   27    91    >   INIT_FCALL                                               'array_splice'
         92        SEND_REF                                                 !5
         93        SEND_VAR                                                 !3
         94        SEND_VAL                                                 1
         95        INIT_FCALL                                               'array_fill'
         96        SEND_VAL                                                 0
         97        SUB                                              ~49     !6, 1
         98        SUB                                              ~50     8, ~49
         99        SEND_VAL                                                 ~50
        100        SEND_VAL                                                 '0000'
        101        DO_ICALL                                         $51     
        102        SEND_VAR                                                 $51
        103        DO_ICALL                                                 
   25   104    > > JMP                                                      ->87
        105    >   FE_FREE                                                  $45
   31   106    > > FE_RESET_RW                                      $53     !5, ->114
        107    > > FE_FETCH_RW                                              $53, !7, ->114
   32   108    >   INIT_FCALL                                               'sprintf'
        109        SEND_VAL                                                 '%2504s'
        110        SEND_VAR                                                 !7
        111        DO_ICALL                                         $54     
        112        ASSIGN                                                   !7, $54
   31   113      > JMP                                                      ->107
        114    >   FE_FREE                                                  $53
   34   115        INIT_FCALL                                               'implode'
        116        SEND_VAL                                                 ''
        117        SEND_VAR                                                 !5
        118        DO_ICALL                                         $56     
        119      > RETURN                                                   $56
        120*       JMP                                                      ->122
   36   121    > > RETURN                                                   <false>
   38   122*     > RETURN                                                   null

End of function normalise_ip

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.18 ms | 1411 KiB | 35 Q