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://', 'https://', '../', './', '//', '/' ); foreach($search as $s) { $count = 0; $url = preg_replace('/^' . preg_quote($s, '/') . '/', $baseUrl, $url, 1, $count); if ($count == 1) { break; } } var_dump($url); return $url; $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/CqeSi
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%2FCqeSi%3A19%240'
          4        BIND_LEXICAL                                             ~7, !2
          5        ASSIGN                                                   !3, ~7
   77     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
   81    12        INIT_FCALL                                               'var_dump'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FCqeSi%3A19%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 35
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 35
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 34
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 35
Branch analysis from position: 35
filename:       /in/CqeSi
function name:  {closure}
number of ops:  86
compiled vars:  !0 = $matches, !1 = $baseUrl, !2 = $srcTag, !3 = $url, !4 = $search, !5 = $s, !6 = $count, !7 = $path, !8 = $query, !9 = $fragment, !10 = $resultUrl
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   21     2        FETCH_DIM_R                                      ~11     !0, 0
          3        ASSIGN                                                   !2, ~11
   22     4        FETCH_DIM_R                                      ~13     !0, 1
          5        ASSIGN                                                   !3, ~13
   26     6        INIT_ARRAY                                       ~15     !1
   27     7        ADD_ARRAY_ELEMENT                                ~15     'http%3A%2F%2F'
   28     8        ADD_ARRAY_ELEMENT                                ~15     'https%3A%2F%2F'
   29     9        ADD_ARRAY_ELEMENT                                ~15     '..%2F'
   30    10        ADD_ARRAY_ELEMENT                                ~15     '.%2F'
   31    11        ADD_ARRAY_ELEMENT                                ~15     '%2F%2F'
   32    12        ADD_ARRAY_ELEMENT                                ~15     '%2F'
   25    13        ASSIGN                                                   !4, ~15
   36    14      > FE_RESET_R                                       $17     !4, ->35
         15    > > FE_FETCH_R                                               $17, !5, ->35
   37    16    >   ASSIGN                                                   !6, 0
   38    17        INIT_FCALL                                               'preg_replace'
         18        INIT_FCALL                                               'preg_quote'
         19        SEND_VAR                                                 !5
         20        SEND_VAL                                                 '%2F'
         21        DO_ICALL                                         $19     
         22        CONCAT                                           ~20     '%2F%5E', $19
         23        CONCAT                                           ~21     ~20, '%2F'
         24        SEND_VAL                                                 ~21
         25        SEND_VAR                                                 !1
         26        SEND_VAR                                                 !3
         27        SEND_VAL                                                 1
         28        SEND_REF                                                 !6
         29        DO_ICALL                                         $22     
         30        ASSIGN                                                   !3, $22
   39    31        IS_EQUAL                                                 !6, 1
         32      > JMPZ                                                     ~24, ->34
   40    33    > > JMP                                                      ->35
   36    34    > > JMP                                                      ->15
         35    >   FE_FREE                                                  $17
   44    36        INIT_FCALL                                               'var_dump'
         37        SEND_VAR                                                 !3
         38        DO_ICALL                                                 
   47    39      > RETURN                                                   !3
   49    40*       INIT_FCALL                                               'preg_replace'
         41*       SEND_VAL                                                 '%2F%5E%5B%5E%5C%2F%5D%5C%2F%2F'
         42*       SEND_VAL                                                 '%2F'
         43*       SEND_VAR                                                 !3
         44*       DO_ICALL                                         $26     
         45*       ASSIGN                                                   !3, $26
   52    46*       INIT_FCALL                                               'parse_url'
         47*       SEND_VAR                                                 !3
         48*       SEND_VAL                                                 5
         49*       DO_ICALL                                         $28     
         50*       ASSIGN                                                   !7, $28
   57    51*       INIT_FCALL                                               'parse_url'
         52*       SEND_VAR                                                 !3
         53*       SEND_VAL                                                 6
         54*       DO_ICALL                                         $30     
         55*       ASSIGN                                                   !8, $30
   58    56*       ISSET_ISEMPTY_CV                                 ~32     !8
         57*       BOOL_NOT                                         ~33     ~32
         58*       JMPZ                                                     ~33, ->61
   59    59*       CONCAT                                           ~34     '%3F', !8
         60*       ASSIGN_OP                                     8          !7, ~34
   63    61*       INIT_FCALL                                               'parse_url'
         62*       SEND_VAR                                                 !3
         63*       SEND_VAL                                                 7
         64*       DO_ICALL                                         $36     
         65*       ASSIGN                                                   !9, $36
   64    66*       ISSET_ISEMPTY_CV                                 ~38     !9
         67*       BOOL_NOT                                         ~39     ~38
         68*       JMPZ                                                     ~39, ->71
   65    69*       CONCAT                                           ~40     '%23', !9
         70*       ASSIGN_OP                                     8          !7, ~40
   69    71*       CONCAT                                           ~42     'http%3A%2F%2F', !1
         72*       CONCAT                                           ~43     ~42, '%2F'
         73*       INIT_FCALL                                               'ltrim'
         74*       SEND_VAR                                                 !7
         75*       SEND_VAL                                                 '%2F'
         76*       DO_ICALL                                         $44     
         77*       CONCAT                                           ~45     ~43, $44
         78*       ASSIGN                                                   !10, ~45
   73    79*       INIT_FCALL                                               'str_replace'
         80*       SEND_VAR                                                 !3
         81*       SEND_VAR                                                 !10
         82*       SEND_VAR                                                 !2
         83*       DO_ICALL                                         $47     
         84*       RETURN                                                   $47
   75    85*     > RETURN                                                   null

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

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.66 ms | 1409 KiB | 27 Q