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> 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 = 'http://' . $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( '../', './' ); $resultUrl = preg_replace($search, '/', $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/PsOIW
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'
   17     2        ASSIGN                                                   !2, 'www.sape.ru'
   19     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPsOIW%3A19%240'
          4        BIND_LEXICAL                                             ~7, !2
          5        ASSIGN                                                   !3, ~7
   94     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
   98    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FPsOIW%3A19%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 44
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 44
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 43
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 49
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 44
Branch analysis from position: 44
filename:       /in/PsOIW
function name:  {closure}
number of ops:  111
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
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   21     2        FETCH_DIM_R                                      ~12     !0, 0
          3        ASSIGN                                                   !2, ~12
   22     4        FETCH_DIM_R                                      ~14     !0, 1
          5        ASSIGN                                                   !3, ~14
   25     6        INIT_ARRAY                                       ~16     !1
   26     7        CONCAT                                           ~17     'http%3A%2F%2F', !1
          8        ADD_ARRAY_ELEMENT                                ~16     ~17
   27     9        CONCAT                                           ~18     'https%3A%2F%2F', !1
         10        ADD_ARRAY_ELEMENT                                ~16     ~18
   28    11        ADD_ARRAY_ELEMENT                                ~16     'http%3A%2F%2F'
   29    12        ADD_ARRAY_ELEMENT                                ~16     'https%3A%2F%2F'
   30    13        ADD_ARRAY_ELEMENT                                ~16     '..%2F'
   31    14        ADD_ARRAY_ELEMENT                                ~16     '.%2F'
   32    15        ADD_ARRAY_ELEMENT                                ~16     '%2F%2F'
   33    16        ADD_ARRAY_ELEMENT                                ~16     '%2F'
   24    17        ASSIGN                                                   !4, ~16
   37    18        CONCAT                                           ~20     'http%3A%2F%2F', !1
         19        CONCAT                                           ~21     ~20, '%2F'
         20        ASSIGN                                                   !5, ~21
   39    21        ASSIGN                                                   !6, !3
   40    22        ASSIGN                                                   !7, 0
   42    23      > FE_RESET_R                                       $25     !4, ->44
         24    > > FE_FETCH_R                                               $25, !8, ->44
   43    25    >   ASSIGN                                                   !7, 0
   44    26        INIT_FCALL                                               'preg_replace'
         27        INIT_FCALL                                               'preg_quote'
         28        SEND_VAR                                                 !8
         29        SEND_VAL                                                 '%2F'
         30        DO_ICALL                                         $27     
         31        CONCAT                                           ~28     '%2F%5E', $27
         32        CONCAT                                           ~29     ~28, '%2F'
         33        SEND_VAL                                                 ~29
         34        SEND_VAR                                                 !5
         35        SEND_VAR                                                 !3
         36        SEND_VAL                                                 1
         37        SEND_REF                                                 !7
         38        DO_ICALL                                         $30     
         39        ASSIGN                                                   !6, $30
   45    40        IS_EQUAL                                                 !7, 1
         41      > JMPZ                                                     ~32, ->43
   46    42    > > JMP                                                      ->44
   42    43    > > JMP                                                      ->24
         44    >   FE_FREE                                                  $25
   50    45        BOOL_NOT                                         ~33     !7
         46      > JMPZ                                                     ~33, ->49
   51    47    >   CONCAT                                           ~34     !5, !3
         48        ASSIGN                                                   !6, ~34
   54    49    >   ASSIGN                                                   !4, <array>
   59    50        INIT_FCALL                                               'preg_replace'
         51        SEND_VAR                                                 !4
         52        SEND_VAL                                                 '%2F'
         53        SEND_VAR                                                 !6
         54        DO_ICALL                                         $37     
         55        ASSIGN                                                   !6, $37
   61    56        INIT_FCALL                                               'var_dump'
         57        SEND_VAR                                                 !6
         58        DO_ICALL                                                 
   64    59        INIT_FCALL                                               'str_replace'
         60        SEND_VAR                                                 !3
         61        SEND_VAR                                                 !6
         62        SEND_VAR                                                 !2
         63        DO_ICALL                                         $40     
         64      > RETURN                                                   $40
   66    65*       INIT_FCALL                                               'preg_replace'
         66*       SEND_VAL                                                 '%2F%5E%5B%5E%5C%2F%5D%5C%2F%2F'
         67*       SEND_VAL                                                 '%2F'
         68*       SEND_VAR                                                 !3
         69*       DO_ICALL                                         $41     
         70*       ASSIGN                                                   !3, $41
   69    71*       INIT_FCALL                                               'parse_url'
         72*       SEND_VAR                                                 !3
         73*       SEND_VAL                                                 5
         74*       DO_ICALL                                         $43     
         75*       ASSIGN                                                   !9, $43
   74    76*       INIT_FCALL                                               'parse_url'
         77*       SEND_VAR                                                 !3
         78*       SEND_VAL                                                 6
         79*       DO_ICALL                                         $45     
         80*       ASSIGN                                                   !10, $45
   75    81*       ISSET_ISEMPTY_CV                                 ~47     !10
         82*       BOOL_NOT                                         ~48     ~47
         83*       JMPZ                                                     ~48, ->86
   76    84*       CONCAT                                           ~49     '%3F', !10
         85*       ASSIGN_OP                                     8          !9, ~49
   80    86*       INIT_FCALL                                               'parse_url'
         87*       SEND_VAR                                                 !3
         88*       SEND_VAL                                                 7
         89*       DO_ICALL                                         $51     
         90*       ASSIGN                                                   !11, $51
   81    91*       ISSET_ISEMPTY_CV                                 ~53     !11
         92*       BOOL_NOT                                         ~54     ~53
         93*       JMPZ                                                     ~54, ->96
   82    94*       CONCAT                                           ~55     '%23', !11
         95*       ASSIGN_OP                                     8          !9, ~55
   86    96*       CONCAT                                           ~57     'http%3A%2F%2F', !1
         97*       CONCAT                                           ~58     ~57, '%2F'
         98*       INIT_FCALL                                               'ltrim'
         99*       SEND_VAR                                                 !9
        100*       SEND_VAL                                                 '%2F'
        101*       DO_ICALL                                         $59     
        102*       CONCAT                                           ~60     ~58, $59
        103*       ASSIGN                                                   !6, ~60
   90   104*       INIT_FCALL                                               'str_replace'
        105*       SEND_VAR                                                 !3
        106*       SEND_VAR                                                 !6
        107*       SEND_VAR                                                 !2
        108*       DO_ICALL                                         $62     
        109*       RETURN                                                   $62
   92   110*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPsOIW%3A19%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
268.34 ms | 1408 KiB | 28 Q