3v4l.org

run code in 300+ PHP versions simultaneously
<?php $patterns = array(); $patterns[]= '/(?:src|href)\=(?:\\\'|")([^\\\'"]+)(?:\\\'|")/'; $patterns[]= '/background\s*\:\s*url\s*\(\s*(?:\\\'|")([^\\\'"]+)(?:\\\'|")\s*\)/'; $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> <img src='path/to/file.png' alt=''></a> <span style="background :url('//path/to/url.jpg' )"> <img src="//club.sape.ru/upload/uf/85a/85ac0401b8f233b35b63cec0364778ba.jpg" width="25"/> SRC; $baseUrl = 'www.sape.ru'; $callback = function($matches) use ($baseUrl) { $srcTag = $matches[0]; $url = $matches[1]; $search = '/^(http:|https:|)\/\/\w*\./'; $isAbsolute = preg_match($search, $url); //УРЛ абсолютный - ничего не меняем if ($isAbsolute) { return $srcTag; } $search = array( '../', './', '/' ); $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); }; $source = preg_replace_callback($patterns, $callback, $source); var_dump($source);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rScp6
function name:  (null)
number of ops:  20
compiled vars:  !0 = $patterns, !1 = $source, !2 = $baseUrl, !3 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN_DIM                                               !0
          2        OP_DATA                                                  '%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'
    7     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  '%2Fbackground%5Cs%2A%5C%3A%5Cs%2Aurl%5Cs%2A%5C%28%5Cs%2A%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%5Cs%2A%5C%29%2F'
    9     5        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%3Cimg+src%3D%27path%2Fto%2Ffile.png%27+alt%3D%27%27%3E%3C%2Fa%3E%0A%0A%3Cspan+style%3D%22background++%3Aurl%28%27%2F%2Fpath%2Fto%2Furl.jpg%27+%29%22%3E%0A%0A%3Cimg+src%3D%22%2F%2Fclub.sape.ru%2Fupload%2Fuf%2F85a%2F85ac0401b8f233b35b63cec0364778ba.jpg%22+width%3D%2225%22%2F%3E%0A'
   29     6        ASSIGN                                                   !2, 'www.sape.ru'
   31     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FrScp6%3A31%240'
          8        BIND_LEXICAL                                             ~9, !2
          9        ASSIGN                                                   !3, ~9
   90    10        INIT_FCALL                                               'preg_replace_callback'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $11     
         15        ASSIGN                                                   !1, $11
   94    16        INIT_FCALL                                               'var_dump'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

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

End of function %00%7Bclosure%7D%2Fin%2FrScp6%3A31%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.32 ms | 1404 KiB | 25 Q