3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pattern = '/(?:src|href)\=(?:\\\'|")([^\\\'"]+)(?:\\\'|")/'; $source = <<<SRC <a href="http://www.sape.ru">some text</a> <img src='logo.jpg' alt=''></a> <img src='../../logo.jpg' alt=''></a> <img src='/path/to/file' alt=''></a> <img src='./path/to/file' alt=''></a> <img src='www.sape.ru/path/to/file' alt=''></a> SRC; $baseUrl = 'www.sape.ru'; $callback = function($matches) use ($baseUrl) { $srcTag = $matches[0]; $url = $matches[1]; $search = array( $baseUrl, 'http://' . $baseUrl, 'https://' . $baseUrl, 'http://', 'https://', '../', './', '//', '/' ); $baseUrlReplace = $baseUrl . '/'; $resultUrl = $url; $count = 0; foreach($search as $s) { $count = 0; $resultUrl = preg_replace('/^' . preg_quote($s, '/') . '/', $baseUrlReplace, $url, 1, $count); if ($count == 1) { break; } } if (!$count) { $resultUrl = $baseUrlReplace . $url; } $search = array( '../', './' ); foreach($search as $s) { $resultUrl = preg_replace('/' . preg_quote($s, '/') . '/', '', $resultUrl); } $resultUrl = preg_replace('/\/{2,}/', '/', $resultUrl); $resultUrl = 'http://' . $resultUrl; var_dump($resultUrl); return str_replace($url, $resultUrl, $srcTag); $url = preg_replace('/^[^\/]\//', '/', $url); $path = parse_url($url, PHP_URL_PATH); //if (empty($path)) { // $path = '/'; //} $query = parse_url($url, PHP_URL_QUERY); if (!empty($query)) { $path.= '?' . $query; } $fragment = parse_url($url, PHP_URL_FRAGMENT); if (!empty($fragment)) { $path.= '#' . $fragment; } $resultUrl = 'http://' . $baseUrl . '/' . ltrim($path, '/'); //var_dump($resultUrl); return str_replace($url, $resultUrl, $srcTag); }; $source = preg_replace_callback($pattern, $callback, $source); var_dump($source);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bk4HJ
function name:  (null)
number of ops:  16
compiled vars:  !0 = $pattern, !1 = $source, !2 = $baseUrl, !3 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%2F%28%3F%3Asrc%7Chref%29%5C%3D%28%3F%3A%5C%27%7C%22%29%28%5B%5E%5C%27%22%5D%2B%29%28%3F%3A%5C%27%7C%22%29%2F'
    5     1        ASSIGN                                                   !1, '%0A%3Ca+href%3D%22http%3A%2F%2Fwww.sape.ru%22%3Esome+text%3C%2Fa%3E%0A%0A%3Cimg+src%3D%27logo.jpg%27+alt%3D%27%27%3E%3C%2Fa%3E%0A%0A%3Cimg+src%3D%27..%2F..%2Flogo.jpg%27+alt%3D%27%27%3E%3C%2Fa%3E%0A%3Cimg+src%3D%27%2Fpath%2Fto%2Ffile%27+alt%3D%27%27%3E%3C%2Fa%3E%0A%0A%3Cimg+src%3D%27.%2Fpath%2Fto%2Ffile%27+alt%3D%27%27%3E%3C%2Fa%3E%0A%3Cimg+src%3D%27www.sape.ru%2Fpath%2Fto%2Ffile%27+alt%3D%27%27%3E%3C%2Fa%3E%0A'
   20     2        ASSIGN                                                   !2, 'www.sape.ru'
   22     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fbk4HJ%3A22%240'
          4        BIND_LEXICAL                                             ~7, !2
          5        ASSIGN                                                   !3, ~7
  103     6        INIT_FCALL                                               'preg_replace_callback'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !3
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $9      
         11        ASSIGN                                                   !1, $9
  107    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2Fbk4HJ%3A22%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 43
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 43
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 42
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 64
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 64
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
Branch analysis from position: 48
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 43
Branch analysis from position: 43
filename:       /in/bk4HJ
function name:  {closure}
number of ops:  128
compiled vars:  !0 = $matches, !1 = $baseUrl, !2 = $srcTag, !3 = $url, !4 = $search, !5 = $baseUrlReplace, !6 = $resultUrl, !7 = $count, !8 = $s, !9 = $path, !10 = $query, !11 = $fragment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   24     2        FETCH_DIM_R                                      ~12     !0, 0
          3        ASSIGN                                                   !2, ~12
   25     4        FETCH_DIM_R                                      ~14     !0, 1
          5        ASSIGN                                                   !3, ~14
   28     6        INIT_ARRAY                                       ~16     !1
   29     7        CONCAT                                           ~17     'http%3A%2F%2F', !1
          8        ADD_ARRAY_ELEMENT                                ~16     ~17
   30     9        CONCAT                                           ~18     'https%3A%2F%2F', !1
         10        ADD_ARRAY_ELEMENT                                ~16     ~18
   31    11        ADD_ARRAY_ELEMENT                                ~16     'http%3A%2F%2F'
   32    12        ADD_ARRAY_ELEMENT                                ~16     'https%3A%2F%2F'
   33    13        ADD_ARRAY_ELEMENT                                ~16     '..%2F'
   34    14        ADD_ARRAY_ELEMENT                                ~16     '.%2F'
   35    15        ADD_ARRAY_ELEMENT                                ~16     '%2F%2F'
   36    16        ADD_ARRAY_ELEMENT                                ~16     '%2F'
   27    17        ASSIGN                                                   !4, ~16
   40    18        CONCAT                                           ~20     !1, '%2F'
         19        ASSIGN                                                   !5, ~20
   42    20        ASSIGN                                                   !6, !3
   43    21        ASSIGN                                                   !7, 0
   45    22      > FE_RESET_R                                       $24     !4, ->43
         23    > > FE_FETCH_R                                               $24, !8, ->43
   46    24    >   ASSIGN                                                   !7, 0
   47    25        INIT_FCALL                                               'preg_replace'
         26        INIT_FCALL                                               'preg_quote'
         27        SEND_VAR                                                 !8
         28        SEND_VAL                                                 '%2F'
         29        DO_ICALL                                         $26     
         30        CONCAT                                           ~27     '%2F%5E', $26
         31        CONCAT                                           ~28     ~27, '%2F'
         32        SEND_VAL                                                 ~28
         33        SEND_VAR                                                 !5
         34        SEND_VAR                                                 !3
         35        SEND_VAL                                                 1
         36        SEND_REF                                                 !7
         37        DO_ICALL                                         $29     
         38        ASSIGN                                                   !6, $29
   48    39        IS_EQUAL                                                 !7, 1
         40      > JMPZ                                                     ~31, ->42
   49    41    > > JMP                                                      ->43
   45    42    > > JMP                                                      ->23
         43    >   FE_FREE                                                  $24
   53    44        BOOL_NOT                                         ~32     !7
         45      > JMPZ                                                     ~32, ->48
   54    46    >   CONCAT                                           ~33     !5, !3
         47        ASSIGN                                                   !6, ~33
   57    48    >   ASSIGN                                                   !4, <array>
   62    49      > FE_RESET_R                                       $36     !4, ->64
         50    > > FE_FETCH_R                                               $36, !8, ->64
   63    51    >   INIT_FCALL                                               'preg_replace'
         52        INIT_FCALL                                               'preg_quote'
         53        SEND_VAR                                                 !8
         54        SEND_VAL                                                 '%2F'
         55        DO_ICALL                                         $37     
         56        CONCAT                                           ~38     '%2F', $37
         57        CONCAT                                           ~39     ~38, '%2F'
         58        SEND_VAL                                                 ~39
         59        SEND_VAL                                                 ''
         60        SEND_VAR                                                 !6
         61        DO_ICALL                                         $40     
         62        ASSIGN                                                   !6, $40
   62    63      > JMP                                                      ->50
         64    >   FE_FREE                                                  $36
   66    65        INIT_FCALL                                               'preg_replace'
         66        SEND_VAL                                                 '%2F%5C%2F%7B2%2C%7D%2F'
         67        SEND_VAL                                                 '%2F'
         68        SEND_VAR                                                 !6
         69        DO_ICALL                                         $42     
         70        ASSIGN                                                   !6, $42
   68    71        CONCAT                                           ~44     'http%3A%2F%2F', !6
         72        ASSIGN                                                   !6, ~44
   70    73        INIT_FCALL                                               'var_dump'
         74        SEND_VAR                                                 !6
         75        DO_ICALL                                                 
   73    76        INIT_FCALL                                               'str_replace'
         77        SEND_VAR                                                 !3
         78        SEND_VAR                                                 !6
         79        SEND_VAR                                                 !2
         80        DO_ICALL                                         $47     
         81      > RETURN                                                   $47
   75    82*       INIT_FCALL                                               'preg_replace'
         83*       SEND_VAL                                                 '%2F%5E%5B%5E%5C%2F%5D%5C%2F%2F'
         84*       SEND_VAL                                                 '%2F'
         85*       SEND_VAR                                                 !3
         86*       DO_ICALL                                         $48     
         87*       ASSIGN                                                   !3, $48
   78    88*       INIT_FCALL                                               'parse_url'
         89*       SEND_VAR                                                 !3
         90*       SEND_VAL                                                 5
         91*       DO_ICALL                                         $50     
         92*       ASSIGN                                                   !9, $50
   83    93*       INIT_FCALL                                               'parse_url'
         94*       SEND_VAR                                                 !3
         95*       SEND_VAL                                                 6
         96*       DO_ICALL                                         $52     
         97*       ASSIGN                                                   !10, $52
   84    98*       ISSET_ISEMPTY_CV                                 ~54     !10
         99*       BOOL_NOT                                         ~55     ~54
        100*       JMPZ                                                     ~55, ->103
   85   101*       CONCAT                                           ~56     '%3F', !10
        102*       ASSIGN_OP                                     8          !9, ~56
   89   103*       INIT_FCALL                                               'parse_url'
        104*       SEND_VAR                                                 !3
        105*       SEND_VAL                                                 7
        106*       DO_ICALL                                         $58     
        107*       ASSIGN                                                   !11, $58
   90   108*       ISSET_ISEMPTY_CV                                 ~60     !11
        109*       BOOL_NOT                                         ~61     ~60
        110*       JMPZ                                                     ~61, ->113
   91   111*       CONCAT                                           ~62     '%23', !11
        112*       ASSIGN_OP                                     8          !9, ~62
   95   113*       CONCAT                                           ~64     'http%3A%2F%2F', !1
        114*       CONCAT                                           ~65     ~64, '%2F'
        115*       INIT_FCALL                                               'ltrim'
        116*       SEND_VAR                                                 !9
        117*       SEND_VAL                                                 '%2F'
        118*       DO_ICALL                                         $66     
        119*       CONCAT                                           ~67     ~65, $66
        120*       ASSIGN                                                   !6, ~67
   99   121*       INIT_FCALL                                               'str_replace'
        122*       SEND_VAR                                                 !3
        123*       SEND_VAR                                                 !6
        124*       SEND_VAR                                                 !2
        125*       DO_ICALL                                         $69     
        126*       RETURN                                                   $69
  101   127*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fbk4HJ%3A22%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.06 ms | 1400 KiB | 27 Q