3v4l.org

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

End of function normalise_ip

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.68 ms | 1414 KiB | 35 Q