3v4l.org

run code in 300+ PHP versions simultaneously
<?php $url = 'https://example.com/folder1/folder2/style.css'; $paths = [ 'img/example1.png', '/img/example2.png', '../img/example3.png', '../../img/example4.png', 'https://example.com/folder1/folder2/example5.png' ]; $folders = explode('/', trim(parse_url($url, PHP_URL_PATH), '/')); array_pop($folders); $prefix = explode('/' . $folders[0] . '/', $url)[0]; // need to be improved using parse_url to re-build // properly the url with the correct syntax for each scheme. function getURLFromPath($path, $prefix, $folders) { if ( parse_url($path, PHP_URL_SCHEME) ) return $path; foreach (explode('/', ltrim($path, '/')) as $item) { if ( $item === '..' ) { array_pop($folders); } elseif ( $item === '.' ) { } else { $folders[] = $item; } } return $prefix . '/' . implode('/', $folders); } foreach ($paths as $path) { echo getURLFromPath($path, $prefix, $folders), PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 37
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 37
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/R0G3G
function name:  (null)
number of ops:  39
compiled vars:  !0 = $url, !1 = $paths, !2 = $folders, !3 = $prefix, !4 = $path
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'https%3A%2F%2Fexample.com%2Ffolder1%2Ffolder2%2Fstyle.css'
    5     1        ASSIGN                                                   !1, <array>
   11     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '%2F'
          4        INIT_FCALL                                               'trim'
          5        INIT_FCALL                                               'parse_url'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 5
          8        DO_ICALL                                         $7      
          9        SEND_VAR                                                 $7
         10        SEND_VAL                                                 '%2F'
         11        DO_ICALL                                         $8      
         12        SEND_VAR                                                 $8
         13        DO_ICALL                                         $9      
         14        ASSIGN                                                   !2, $9
   12    15        INIT_FCALL                                               'array_pop'
         16        SEND_REF                                                 !2
         17        DO_ICALL                                                 
   13    18        INIT_FCALL                                               'explode'
         19        FETCH_DIM_R                                      ~12     !2, 0
         20        CONCAT                                           ~13     '%2F', ~12
         21        CONCAT                                           ~14     ~13, '%2F'
         22        SEND_VAL                                                 ~14
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $15     
         25        FETCH_DIM_R                                      ~16     $15, 0
         26        ASSIGN                                                   !3, ~16
   32    27      > FE_RESET_R                                       $18     !1, ->37
         28    > > FE_FETCH_R                                               $18, !4, ->37
   33    29    >   INIT_FCALL                                               'geturlfrompath'
         30        SEND_VAR                                                 !4
         31        SEND_VAR                                                 !3
         32        SEND_VAR                                                 !2
         33        DO_FCALL                                      0  $19     
         34        ECHO                                                     $19
         35        ECHO                                                     '%0A'
   32    36      > JMP                                                      ->28
         37    >   FE_FREE                                                  $18
   34    38      > RETURN                                                   1

Function geturlfrompath:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 31
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 31
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/R0G3G
function name:  getURLFromPath
number of ops:  40
compiled vars:  !0 = $path, !1 = $prefix, !2 = $folders, !3 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   17     3        INIT_FCALL                                               'parse_url'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 0
          6        DO_ICALL                                         $4      
          7      > JMPZ                                                     $4, ->9
   18     8    > > RETURN                                                   !0
   20     9    >   INIT_FCALL                                               'explode'
         10        SEND_VAL                                                 '%2F'
         11        INIT_FCALL                                               'ltrim'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 '%2F'
         14        DO_ICALL                                         $5      
         15        SEND_VAR                                                 $5
         16        DO_ICALL                                         $6      
         17      > FE_RESET_R                                       $7      $6, ->31
         18    > > FE_FETCH_R                                               $7, !3, ->31
   21    19    >   IS_IDENTICAL                                             !3, '..'
         20      > JMPZ                                                     ~8, ->25
   22    21    >   INIT_FCALL                                               'array_pop'
         22        SEND_REF                                                 !2
         23        DO_ICALL                                                 
   21    24      > JMP                                                      ->30
   23    25    >   IS_IDENTICAL                                             !3, '.'
         26      > JMPZ                                                     ~10, ->28
         27    > > JMP                                                      ->30
   25    28    >   ASSIGN_DIM                                               !2
         29        OP_DATA                                                  !3
   20    30    > > JMP                                                      ->18
         31    >   FE_FREE                                                  $7
   29    32        CONCAT                                           ~12     !1, '%2F'
         33        INIT_FCALL                                               'implode'
         34        SEND_VAL                                                 '%2F'
         35        SEND_VAR                                                 !2
         36        DO_ICALL                                         $13     
         37        CONCAT                                           ~14     ~12, $13
         38      > RETURN                                                   ~14
   30    39*     > RETURN                                                   null

End of function geturlfrompath

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.29 ms | 1014 KiB | 20 Q