3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML Meet God's General Kathryn Kuhlman. <br> <img class="lazy_responsive" title="Kathryn Kuhlman - iUseFaith.com" src="https://www.iusefaith.com/ojm_thumbnail/1000/32f808f79011a7c0bd1ffefc1365c856.jpg" alt="Kathryn Kuhlman - iUseFaith.com" width="1600" height="517" /> <br> Follow <a href="https://www.iusefaith.com/en-354" title="Kathryn Kuhlman">Kathryn Kuhlman</a> <br> Max KANTCHEDE HTML; $keywords = [ 'Kathryn Kuhlman' => 'https://www.example.com/en-354', 'Max KANTCHEDE' => 'https://www.example.com/MaxKANTCHEDE', 'eneral' => 'https://www.example.com/this-is-not-used', ]; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $lookup = []; $regexNeedles = []; foreach ($keywords as $name => $link) { $lookup[strtolower($name)] = $link; $regexNeedles[] = preg_quote($name, '~'); } $pattern = '~\b(' . implode('|', $regexNeedles) . ')\b~i' ; foreach($xpath->query('//*[not(self::img or self::a)]/text()') as $textNode) { $newNodes = []; $hasReplacement = false; foreach (preg_split($pattern, $textNode->nodeValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $fragment) { $fragmentLower = strtolower($fragment); if (isset($lookup[$fragmentLower])) { $hasReplacement = true; $a = $dom->createElement('a'); $a->setAttribute('href', $lookup[$fragmentLower]); $a->setAttribute('title', $fragment); $a->nodeValue = $fragment; $newNodes[] = $a; } else { $newNodes[] = $dom->createTextNode($fragment); } } if ($hasReplacement) { $newFragment = $dom->createDocumentFragment(); foreach ($newNodes as $newNode) { $newFragment->appendChild($newNode); } $textNode->parentNode->replaceChild($newFragment, $textNode); } } echo substr(trim($dom->saveHTML()), 3, -4);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 33
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 33
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 33
2 jumps found. (Code = 77) Position 1 = 45, Position 2 = 107
Branch analysis from position: 45
2 jumps found. (Code = 78) Position 1 = 46, Position 2 = 107
Branch analysis from position: 46
2 jumps found. (Code = 77) Position 1 = 56, Position 2 = 89
Branch analysis from position: 56
2 jumps found. (Code = 78) Position 1 = 57, Position 2 = 89
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 83
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 88
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
Branch analysis from position: 89
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 106
Branch analysis from position: 91
2 jumps found. (Code = 77) Position 1 = 95, Position 2 = 100
Branch analysis from position: 95
2 jumps found. (Code = 78) Position 1 = 96, Position 2 = 100
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 95
Branch analysis from position: 95
Branch analysis from position: 100
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 100
Branch analysis from position: 106
Branch analysis from position: 89
Branch analysis from position: 107
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 107
Branch analysis from position: 33
filename:       /in/WUQ6k
function name:  (null)
number of ops:  120
compiled vars:  !0 = $html, !1 = $keywords, !2 = $dom, !3 = $xpath, !4 = $lookup, !5 = $regexNeedles, !6 = $link, !7 = $name, !8 = $pattern, !9 = $textNode, !10 = $newNodes, !11 = $hasReplacement, !12 = $fragment, !13 = $fragmentLower, !14 = $a, !15 = $newFragment, !16 = $newNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Meet+God%27s+General+Kathryn+Kuhlman.+%3Cbr%3E%0A%3Cimg+class%3D%22lazy_responsive%22+title%3D%22Kathryn+Kuhlman+-+iUseFaith.com%22+src%3D%22https%3A%2F%2Fwww.iusefaith.com%2Fojm_thumbnail%2F1000%2F32f808f79011a7c0bd1ffefc1365c856.jpg%22+alt%3D%22Kathryn+Kuhlman+-+iUseFaith.com%22+width%3D%221600%22+height%3D%22517%22+%2F%3E%0A%3Cbr%3E%0AFollow+%3Ca+href%3D%22https%3A%2F%2Fwww.iusefaith.com%2Fen-354%22+title%3D%22Kathryn+Kuhlman%22%3EKathryn+Kuhlman%3C%2Fa%3E%0A%3Cbr%3E%0AMax+KANTCHEDE'
   12     1        ASSIGN                                                   !1, <array>
   18     2        INIT_FCALL                                               'libxml_use_internal_errors'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                                 
   19     5        NEW                                              $20     'DOMDocument'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !2, $20
   20     8        INIT_METHOD_CALL                                         !2, 'loadHTML'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAL_EX                                              8196
         11        DO_FCALL                                      0          
   22    12        NEW                                              $24     'DOMXPath'
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !3, $24
   24    16        ASSIGN                                                   !4, <array>
   25    17        ASSIGN                                                   !5, <array>
   26    18      > FE_RESET_R                                       $29     !1, ->33
         19    > > FE_FETCH_R                                       ~30     $29, !6, ->33
         20    >   ASSIGN                                                   !7, ~30
   27    21        INIT_FCALL                                               'strtolower'
         22        SEND_VAR                                                 !7
         23        DO_ICALL                                         $32     
         24        ASSIGN_DIM                                               !4, $32
         25        OP_DATA                                                  !6
   28    26        INIT_FCALL                                               'preg_quote'
         27        SEND_VAR                                                 !7
         28        SEND_VAL                                                 '%7E'
         29        DO_ICALL                                         $35     
         30        ASSIGN_DIM                                               !5
         31        OP_DATA                                                  $35
   26    32      > JMP                                                      ->19
         33    >   FE_FREE                                                  $29
   30    34        INIT_FCALL                                               'implode'
         35        SEND_VAL                                                 '%7C'
         36        SEND_VAR                                                 !5
         37        DO_ICALL                                         $36     
         38        CONCAT                                           ~37     '%7E%5Cb%28', $36
         39        CONCAT                                           ~38     ~37, '%29%5Cb%7Ei'
         40        ASSIGN                                                   !8, ~38
   32    41        INIT_METHOD_CALL                                         !3, 'query'
         42        SEND_VAL_EX                                              '%2F%2F%2A%5Bnot%28self%3A%3Aimg+or+self%3A%3Aa%29%5D%2Ftext%28%29'
         43        DO_FCALL                                      0  $40     
         44      > FE_RESET_R                                       $41     $40, ->107
         45    > > FE_FETCH_R                                               $41, !9, ->107
   33    46    >   ASSIGN                                                   !10, <array>
   34    47        ASSIGN                                                   !11, <false>
   35    48        INIT_FCALL                                               'preg_split'
         49        SEND_VAR                                                 !8
         50        FETCH_OBJ_R                                      ~44     !9, 'nodeValue'
         51        SEND_VAL                                                 ~44
         52        SEND_VAL                                                 0
         53        SEND_VAL                                                 3
         54        DO_ICALL                                         $45     
         55      > FE_RESET_R                                       $46     $45, ->89
         56    > > FE_FETCH_R                                               $46, !12, ->89
   36    57    >   INIT_FCALL                                               'strtolower'
         58        SEND_VAR                                                 !12
         59        DO_ICALL                                         $47     
         60        ASSIGN                                                   !13, $47
   37    61        ISSET_ISEMPTY_DIM_OBJ                         0          !4, !13
         62      > JMPZ                                                     ~49, ->83
   38    63    >   ASSIGN                                                   !11, <true>
   39    64        INIT_METHOD_CALL                                         !2, 'createElement'
         65        SEND_VAL_EX                                              'a'
         66        DO_FCALL                                      0  $51     
         67        ASSIGN                                                   !14, $51
   40    68        INIT_METHOD_CALL                                         !14, 'setAttribute'
         69        SEND_VAL_EX                                              'href'
         70        CHECK_FUNC_ARG                                           
         71        FETCH_DIM_FUNC_ARG                               $53     !4, !13
         72        SEND_FUNC_ARG                                            $53
         73        DO_FCALL                                      0          
   41    74        INIT_METHOD_CALL                                         !14, 'setAttribute'
         75        SEND_VAL_EX                                              'title'
         76        SEND_VAR_EX                                              !12
         77        DO_FCALL                                      0          
   42    78        ASSIGN_OBJ                                               !14, 'nodeValue'
         79        OP_DATA                                                  !12
   43    80        ASSIGN_DIM                                               !10
         81        OP_DATA                                                  !14
   37    82      > JMP                                                      ->88
   45    83    >   INIT_METHOD_CALL                                         !2, 'createTextNode'
         84        SEND_VAR_EX                                              !12
         85        DO_FCALL                                      0  $59     
         86        ASSIGN_DIM                                               !10
         87        OP_DATA                                                  $59
   35    88    > > JMP                                                      ->56
         89    >   FE_FREE                                                  $46
   48    90      > JMPZ                                                     !11, ->106
   49    91    >   INIT_METHOD_CALL                                         !2, 'createDocumentFragment'
         92        DO_FCALL                                      0  $60     
         93        ASSIGN                                                   !15, $60
   50    94      > FE_RESET_R                                       $62     !10, ->100
         95    > > FE_FETCH_R                                               $62, !16, ->100
   51    96    >   INIT_METHOD_CALL                                         !15, 'appendChild'
         97        SEND_VAR_EX                                              !16
         98        DO_FCALL                                      0          
   50    99      > JMP                                                      ->95
        100    >   FE_FREE                                                  $62
   53   101        FETCH_OBJ_R                                      ~64     !9, 'parentNode'
        102        INIT_METHOD_CALL                                         ~64, 'replaceChild'
        103        SEND_VAR_EX                                              !15
        104        SEND_VAR_EX                                              !9
        105        DO_FCALL                                      0          
   32   106    > > JMP                                                      ->45
        107    >   FE_FREE                                                  $41
   56   108        INIT_FCALL                                               'substr'
        109        INIT_FCALL                                               'trim'
        110        INIT_METHOD_CALL                                         !2, 'saveHTML'
        111        DO_FCALL                                      0  $66     
        112        SEND_VAR                                                 $66
        113        DO_ICALL                                         $67     
        114        SEND_VAR                                                 $67
        115        SEND_VAL                                                 3
        116        SEND_VAL                                                 -4
        117        DO_ICALL                                         $68     
        118        ECHO                                                     $68
        119      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
240.95 ms | 1021 KiB | 20 Q