3v4l.org

run code in 300+ PHP versions simultaneously
<?php function rewrite_relative_links($md, $page, $baselink = null) { if(!$baselink) $baselink = $page->getEntity()->getRelativeLink(); $re = '/ ([^\!]?) # exclude image format \[ (.*?) # link title (non greedy) \] \( (.*?) # link url (non greedy) \) /x'; preg_match_all($re, $md, $matches); // relative path (relative to module base folder), without the filename. // For "sapphire/en/current/topics/templates", this would be "templates" $relativePath = dirname($page->getRelativePath()); if($relativePath == '.') $relativePath = ''; // file base link $fileBaseLink = Director::makeRelative(dirname($page->getPath())); if($matches) { foreach($matches[0] as $i => $match) { $title = $matches[2][$i]; $url = $matches[3][$i]; // Don't process API links if(preg_match('/^api:/', $url)) continue; // Don't process absolute links (based on protocol detection) $urlParts = parse_url($url); if($urlParts && isset($urlParts['scheme'])) continue; // for images we need to use the file base path if(preg_match('/_images/', $url)) { $relativeUrl = Controller::join_links( Director::absoluteBaseURL(), $fileBaseLink, $url ); } else { // Rewrite public URL if(preg_match('/^\//', $url)) { // Absolute: Only path to module base $relativeUrl = Controller::join_links($baselink, $url); } else { // Relative: Include path to module base and any folders $relativeUrl = Controller::join_links($baselink, $relativePath, $url); } } // Resolve relative paths while(strpos($relativeUrl, '..') !== FALSE) { $relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl); } // Replace any double slashes (apart from protocol) $relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl); // Replace in original content $md = str_replace( $match, sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl), $md ); } } return $md; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/luQ67
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E > > RETURN                                                   1

Function rewrite_relative_links:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 126
Branch analysis from position: 35
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 125
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 125
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 51
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 51
2 jumps found. (Code = 46) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 60
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 60
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 74
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 92
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 92
2 jumps found. (Code = 44) Position 1 = 104, Position 2 = 92
Branch analysis from position: 104
Branch analysis from position: 92
Branch analysis from position: 74
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 85
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 58
Branch analysis from position: 125
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 125
Branch analysis from position: 126
Branch analysis from position: 25
Branch analysis from position: 10
filename:       /in/luQ67
function name:  rewrite_relative_links
number of ops:  128
compiled vars:  !0 = $md, !1 = $page, !2 = $baselink, !3 = $re, !4 = $matches, !5 = $relativePath, !6 = $fileBaseLink, !7 = $match, !8 = $i, !9 = $title, !10 = $url, !11 = $urlParts, !12 = $relativeUrl
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
    4     3        BOOL_NOT                                         ~13     !2
          4      > JMPZ                                                     ~13, ->10
          5    >   INIT_METHOD_CALL                                         !1, 'getEntity'
          6        DO_FCALL                                      0  $14     
          7        INIT_METHOD_CALL                                         $14, 'getRelativeLink'
          8        DO_FCALL                                      0  $15     
          9        ASSIGN                                                   !2, $15
    6    10    >   ASSIGN                                                   !3, '%2F%0A%09%09%09%28%5B%5E%5C%21%5D%3F%29+%23+exclude+image+format%0A%09%09%09%5C%5B%0A%09%09%09%09%28.%2A%3F%29+%23+link+title+%28non+greedy%29%0A%09%09%09%5C%5D+%0A%09%09%09%5C%28%0A%09%09%09%09%28.%2A%3F%29+%23+link+url+%28non+greedy%29%0A%09%09%09%5C%29%0A%09%09%2Fx'
   15    11        INIT_FCALL                                               'preg_match_all'
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !0
         14        SEND_REF                                                 !4
         15        DO_ICALL                                                 
   19    16        INIT_FCALL                                               'dirname'
         17        INIT_METHOD_CALL                                         !1, 'getRelativePath'
         18        DO_FCALL                                      0  $19     
         19        SEND_VAR                                                 $19
         20        DO_ICALL                                         $20     
         21        ASSIGN                                                   !5, $20
   20    22        IS_EQUAL                                                 !5, '.'
         23      > JMPZ                                                     ~22, ->25
         24    >   ASSIGN                                                   !5, ''
   23    25    >   INIT_STATIC_METHOD_CALL                                  'Director', 'makeRelative'
         26        INIT_FCALL                                               'dirname'
         27        INIT_METHOD_CALL                                         !1, 'getPath'
         28        DO_FCALL                                      0  $24     
         29        SEND_VAR                                                 $24
         30        DO_ICALL                                         $25     
         31        SEND_VAR_NO_REF_EX                                       $25
         32        DO_FCALL                                      0  $26     
         33        ASSIGN                                                   !6, $26
   25    34      > JMPZ                                                     !4, ->126
   26    35    >   FETCH_DIM_R                                      ~28     !4, 0
         36      > FE_RESET_R                                       $29     ~28, ->125
         37    > > FE_FETCH_R                                       ~30     $29, !7, ->125
         38    >   ASSIGN                                                   !8, ~30
   27    39        FETCH_DIM_R                                      ~32     !4, 2
         40        FETCH_DIM_R                                      ~33     ~32, !8
         41        ASSIGN                                                   !9, ~33
   28    42        FETCH_DIM_R                                      ~35     !4, 3
         43        FETCH_DIM_R                                      ~36     ~35, !8
         44        ASSIGN                                                   !10, ~36
   31    45        INIT_FCALL                                               'preg_match'
         46        SEND_VAL                                                 '%2F%5Eapi%3A%2F'
         47        SEND_VAR                                                 !10
         48        DO_ICALL                                         $38     
         49      > JMPZ                                                     $38, ->51
         50    > > JMP                                                      ->37
   34    51    >   INIT_FCALL                                               'parse_url'
         52        SEND_VAR                                                 !10
         53        DO_ICALL                                         $39     
         54        ASSIGN                                                   !11, $39
   35    55      > JMPZ_EX                                          ~41     !11, ->58
         56    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~42     !11, 'scheme'
         57        BOOL                                             ~41     ~42
         58    > > JMPZ                                                     ~41, ->60
         59    > > JMP                                                      ->37
   38    60    >   INIT_FCALL                                               'preg_match'
         61        SEND_VAL                                                 '%2F_images%2F'
         62        SEND_VAR                                                 !10
         63        DO_ICALL                                         $43     
         64      > JMPZ                                                     $43, ->74
   39    65    >   INIT_STATIC_METHOD_CALL                                  'Controller', 'join_links'
   40    66        INIT_STATIC_METHOD_CALL                                  'Director', 'absoluteBaseURL'
         67        DO_FCALL                                      0  $44     
         68        SEND_VAR_NO_REF_EX                                       $44
         69        SEND_VAR_EX                                              !6
         70        SEND_VAR_EX                                              !10
         71        DO_FCALL                                      0  $45     
   39    72        ASSIGN                                                   !12, $45
         73      > JMP                                                      ->91
   47    74    >   INIT_FCALL                                               'preg_match'
         75        SEND_VAL                                                 '%2F%5E%5C%2F%2F'
         76        SEND_VAR                                                 !10
         77        DO_ICALL                                         $47     
         78      > JMPZ                                                     $47, ->85
   49    79    >   INIT_STATIC_METHOD_CALL                                  'Controller', 'join_links'
         80        SEND_VAR_EX                                              !2
         81        SEND_VAR_EX                                              !10
         82        DO_FCALL                                      0  $48     
         83        ASSIGN                                                   !12, $48
         84      > JMP                                                      ->91
   52    85    >   INIT_STATIC_METHOD_CALL                                  'Controller', 'join_links'
         86        SEND_VAR_EX                                              !2
         87        SEND_VAR_EX                                              !5
         88        SEND_VAR_EX                                              !10
         89        DO_FCALL                                      0  $50     
         90        ASSIGN                                                   !12, $50
   57    91    > > JMP                                                      ->98
   58    92    >   INIT_FCALL                                               'preg_replace'
         93        SEND_VAL                                                 '%2F%5Cw%2B%5C%2F%5C.%5C.%5C%2F%2F'
         94        SEND_VAL                                                 ''
         95        SEND_VAR                                                 !12
         96        DO_ICALL                                         $52     
         97        ASSIGN                                                   !12, $52
   57    98    >   INIT_FCALL                                               'strpos'
         99        SEND_VAR                                                 !12
        100        SEND_VAL                                                 '..'
        101        DO_ICALL                                         $54     
        102        TYPE_CHECK                                  1018          $54
        103      > JMPNZ                                                    ~55, ->92
   62   104    >   INIT_FCALL                                               'preg_replace'
        105        SEND_VAL                                                 '%2F%28%5B%5E%3A%5D%29%5C%2F%7B2%2C%7D%2F'
        106        SEND_VAL                                                 '%241%2F'
        107        SEND_VAR                                                 !12
        108        DO_ICALL                                         $56     
        109        ASSIGN                                                   !12, $56
   65   110        INIT_FCALL                                               'str_replace'
   66   111        SEND_VAR                                                 !7
   67   112        INIT_FCALL                                               'sprintf'
        113        SEND_VAL                                                 '%25s%5B%25s%5D%28%25s%29'
        114        FETCH_DIM_R                                      ~58     !4, 1
        115        FETCH_DIM_R                                      ~59     ~58, !8
        116        SEND_VAL                                                 ~59
        117        SEND_VAR                                                 !9
        118        SEND_VAR                                                 !12
        119        DO_ICALL                                         $60     
        120        SEND_VAR                                                 $60
   68   121        SEND_VAR                                                 !0
        122        DO_ICALL                                         $61     
   65   123        ASSIGN                                                   !0, $61
   26   124      > JMP                                                      ->37
        125    >   FE_FREE                                                  $29
   73   126    > > RETURN                                                   !0
   74   127*     > RETURN                                                   null

End of function rewrite_relative_links

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.52 ms | 1412 KiB | 29 Q