3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Normalise a path, resolving empty, . and .. components, optionally against another path * * @param string $path * @param string $target * @return string */ function resolve_path($path, $relativeBase = null) { // Find separator in use and determine whether output path should be absolute $separator = strpos($path, '\\') !== false || strpos($relativeBase, '\\') !== false ? '\\' : '/'; $isAbsolute = (!isset($relativeBase) && ($path[0] === '/' || $path[0] === '\\')) || (isset($relativeBase) && ($relativeBase[0] === '/' || $relativeBase[0] === '\\')); // Create the base output array $target = $relativeBase !== null ? preg_split('#[\\\\/]+#', $relativeBase, -1, PREG_SPLIT_NO_EMPTY) : []; // Strip empty components and resolve . and .. foreach (preg_split('#[\\\\/]+#', $path, -1, PREG_SPLIT_NO_EMPTY) as $component) { var_dump($component); switch ($component) { case '.': // current directory - do nothing break; case '..': // up a level array_pop($target); break; default: $target[] = $component; break; } } // Add a trailing empty element if path refers to a directory $lastChar = $path[strlen($path) - 1]; if ($lastChar === '/' || $lastChar === '\\') { $target[] = ''; } // Add a leading slash if path is absolute if ($isAbsolute) { array_unshift($target, $separator); } return implode($separator, $target); } $tests = [ '/foo/bar/./../baz' => null, ]; foreach ($tests as $path => $base) { $result = resolve_path($path, $base); echo " Path: $path Base: $base Result: $result "; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 18
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/6sg83
function name:  (null)
number of ops:  20
compiled vars:  !0 = $tests, !1 = $base, !2 = $path, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   ASSIGN                                                   !0, <array>
   55     1      > FE_RESET_R                                       $5      !0, ->18
          2    > > FE_FETCH_R                                       ~6      $5, !1, ->18
          3    >   ASSIGN                                                   !2, ~6
   56     4        INIT_FCALL                                               'resolve_path'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $8      
          8        ASSIGN                                                   !3, $8
   58     9        ROPE_INIT                                     7  ~11     '%0APath%3A+++'
   59    10        ROPE_ADD                                      1  ~11     ~11, !2
         11        ROPE_ADD                                      2  ~11     ~11, '%0ABase%3A+++'
   60    12        ROPE_ADD                                      3  ~11     ~11, !1
         13        ROPE_ADD                                      4  ~11     ~11, '%0AResult%3A+'
   61    14        ROPE_ADD                                      5  ~11     ~11, !3
         15        ROPE_END                                      6  ~10     ~11, '%0A'
         16        ECHO                                                     ~10
   55    17      > JMP                                                      ->2
         18    >   FE_FREE                                                  $5
   63    19      > RETURN                                                   1

Function resolve_path:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
2 jumps found. (Code = 47) Position 1 = 25, Position 2 = 28
Branch analysis from position: 25
2 jumps found. (Code = 47) Position 1 = 30, Position 2 = 40
Branch analysis from position: 30
2 jumps found. (Code = 46) Position 1 = 32, Position 2 = 39
Branch analysis from position: 32
2 jumps found. (Code = 47) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 51
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 77) Position 1 = 60, Position 2 = 79
Branch analysis from position: 60
2 jumps found. (Code = 78) Position 1 = 61, Position 2 = 79
Branch analysis from position: 61
4 jumps found. (Code = 188) Position 1 = 70, Position 2 = 71, Position 3 = 75, Position 4 = 65
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 70
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 71
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 71
Branch analysis from position: 70
Branch analysis from position: 79
2 jumps found. (Code = 47) Position 1 = 86, Position 2 = 88
Branch analysis from position: 86
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 91
Branch analysis from position: 89
2 jumps found. (Code = 43) Position 1 = 92, Position 2 = 96
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 96
Branch analysis from position: 91
Branch analysis from position: 88
Branch analysis from position: 79
Branch analysis from position: 51
2 jumps found. (Code = 77) Position 1 = 60, Position 2 = 79
Branch analysis from position: 60
Branch analysis from position: 79
Branch analysis from position: 38
Branch analysis from position: 39
Branch analysis from position: 40
Branch analysis from position: 28
Branch analysis from position: 29
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
Branch analysis from position: 29
Branch analysis from position: 14
filename:       /in/6sg83
function name:  resolve_path
number of ops:  102
compiled vars:  !0 = $path, !1 = $relativeBase, !2 = $separator, !3 = $isAbsolute, !4 = $target, !5 = $component, !6 = $lastChar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   13     2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 '%5C'
          5        DO_ICALL                                         $7      
          6        TYPE_CHECK                                  1018  ~8      $7
          7      > JMPNZ_EX                                         ~8      ~8, ->14
          8    >   INIT_FCALL                                               'strpos'
          9        SEND_VAR                                                 !1
         10        SEND_VAL                                                 '%5C'
         11        DO_ICALL                                         $9      
         12        TYPE_CHECK                                  1018  ~10     $9
         13        BOOL                                             ~8      ~10
         14    > > JMPZ                                                     ~8, ->17
         15    >   QM_ASSIGN                                        ~11     '%5C'
         16      > JMP                                                      ->18
         17    >   QM_ASSIGN                                        ~11     '%2F'
         18    >   ASSIGN                                                   !2, ~11
   14    19        ISSET_ISEMPTY_CV                                 ~13     !1
         20        BOOL_NOT                                         ~14     ~13
         21      > JMPZ_EX                                          ~14     ~14, ->29
         22    >   FETCH_DIM_R                                      ~15     !0, 0
         23        IS_IDENTICAL                                     ~16     ~15, '%2F'
         24      > JMPNZ_EX                                         ~16     ~16, ->28
         25    >   FETCH_DIM_R                                      ~17     !0, 0
         26        IS_IDENTICAL                                     ~18     ~17, '%5C'
         27        BOOL                                             ~16     ~18
         28    >   BOOL                                             ~14     ~16
         29    > > JMPNZ_EX                                         ~14     ~14, ->40
   15    30    >   ISSET_ISEMPTY_CV                                 ~19     !1
         31      > JMPZ_EX                                          ~19     ~19, ->39
         32    >   FETCH_DIM_R                                      ~20     !1, 0
         33        IS_IDENTICAL                                     ~21     ~20, '%2F'
         34      > JMPNZ_EX                                         ~21     ~21, ->38
         35    >   FETCH_DIM_R                                      ~22     !1, 0
         36        IS_IDENTICAL                                     ~23     ~22, '%5C'
         37        BOOL                                             ~21     ~23
         38    >   BOOL                                             ~19     ~21
         39    >   BOOL                                             ~14     ~19
   14    40    >   ASSIGN                                                   !3, ~14
   18    41        TYPE_CHECK                                  1020          !1
         42      > JMPZ                                                     ~25, ->51
         43    >   INIT_FCALL                                               'preg_split'
         44        SEND_VAL                                                 '%23%5B%5C%5C%2F%5D%2B%23'
         45        SEND_VAR                                                 !1
         46        SEND_VAL                                                 -1
         47        SEND_VAL                                                 1
         48        DO_ICALL                                         $26     
         49        QM_ASSIGN                                        ~27     $26
         50      > JMP                                                      ->52
         51    >   QM_ASSIGN                                        ~27     <array>
         52    >   ASSIGN                                                   !4, ~27
   21    53        INIT_FCALL                                               'preg_split'
         54        SEND_VAL                                                 '%23%5B%5C%5C%2F%5D%2B%23'
         55        SEND_VAR                                                 !0
         56        SEND_VAL                                                 -1
         57        SEND_VAL                                                 1
         58        DO_ICALL                                         $29     
         59      > FE_RESET_R                                       $30     $29, ->79
         60    > > FE_FETCH_R                                               $30, !5, ->79
   22    61    >   INIT_FCALL                                               'var_dump'
         62        SEND_VAR                                                 !5
         63        DO_ICALL                                                 
   23    64      > SWITCH_STRING                                            !5, [ '.':->70, '..':->71, ], ->75
   24    65    >   IS_EQUAL                                                 !5, '.'
         66      > JMPNZ                                                    ~32, ->70
   27    67    >   IS_EQUAL                                                 !5, '..'
         68      > JMPNZ                                                    ~32, ->71
         69    > > JMP                                                      ->75
   25    70    > > JMP                                                      ->78
   28    71    >   INIT_FCALL                                               'array_pop'
         72        SEND_REF                                                 !4
         73        DO_ICALL                                                 
   29    74      > JMP                                                      ->78
   32    75    >   ASSIGN_DIM                                               !4
         76        OP_DATA                                                  !5
   33    77      > JMP                                                      ->78
   21    78    > > JMP                                                      ->60
         79    >   FE_FREE                                                  $30
   38    80        STRLEN                                           ~35     !0
         81        SUB                                              ~36     ~35, 1
         82        FETCH_DIM_R                                      ~37     !0, ~36
         83        ASSIGN                                                   !6, ~37
   39    84        IS_IDENTICAL                                     ~39     !6, '%2F'
         85      > JMPNZ_EX                                         ~39     ~39, ->88
         86    >   IS_IDENTICAL                                     ~40     !6, '%5C'
         87        BOOL                                             ~39     ~40
         88    > > JMPZ                                                     ~39, ->91
   40    89    >   ASSIGN_DIM                                               !4
         90        OP_DATA                                                  ''
   44    91    > > JMPZ                                                     !3, ->96
   45    92    >   INIT_FCALL                                               'array_unshift'
         93        SEND_REF                                                 !4
         94        SEND_VAR                                                 !2
         95        DO_ICALL                                                 
   48    96    >   INIT_FCALL                                               'implode'
         97        SEND_VAR                                                 !2
         98        SEND_VAR                                                 !4
         99        DO_ICALL                                         $43     
        100      > RETURN                                                   $43
   49   101*     > RETURN                                                   null

End of function resolve_path

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
192.75 ms | 1410 KiB | 26 Q