3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<HTML foo <a href='http://test.com'>fóo</a> lórem bár ipsum bar food foo bark. <a>bar</a> not á test HTML; $lookup = [ 'foo' => 'h3', 'bar' => 'h2' ]; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $regexNeedles = []; foreach ($lookup as $word => $tagName) { $regexNeedles[] = preg_quote($word, '~'); } $pattern = '~\b(' . implode('|', $regexNeedles) . ')\b~iu' ; foreach($xpath->query('//*[not(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($lookup[$fragmentLower]); $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(utf8_decode($dom->saveHTML($dom->documentElement))), 3, -4);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 27
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 27
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 36, Position 2 = 90
Branch analysis from position: 36
2 jumps found. (Code = 78) Position 1 = 37, Position 2 = 90
Branch analysis from position: 37
2 jumps found. (Code = 77) Position 1 = 47, Position 2 = 72
Branch analysis from position: 47
2 jumps found. (Code = 78) Position 1 = 48, Position 2 = 72
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 66
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 89
Branch analysis from position: 74
2 jumps found. (Code = 77) Position 1 = 78, Position 2 = 83
Branch analysis from position: 78
2 jumps found. (Code = 78) Position 1 = 79, Position 2 = 83
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 83
Branch analysis from position: 89
Branch analysis from position: 72
Branch analysis from position: 90
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 90
Branch analysis from position: 27
filename:       /in/XXNkq
function name:  (null)
number of ops:  104
compiled vars:  !0 = $html, !1 = $lookup, !2 = $dom, !3 = $xpath, !4 = $regexNeedles, !5 = $tagName, !6 = $word, !7 = $pattern, !8 = $textNode, !9 = $newNodes, !10 = $hasReplacement, !11 = $fragment, !12 = $fragmentLower, !13 = $a, !14 = $newFragment, !15 = $newNode
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, 'foo+%3Ca+href%3D%27http%3A%2F%2Ftest.com%27%3Ef%C3%B3o%3C%2Fa%3E+l%C3%B3rem%0Ab%C3%A1r+ipsum+bar+food+foo+bark.+%3Ca%3Ebar%3C%2Fa%3E+not+%C3%A1+test'
    8     1        ASSIGN                                                       !1, <array>
   13     2        INIT_FCALL                                                   'libxml_use_internal_errors'
          3        SEND_VAL                                                     <true>
          4        DO_ICALL                                                     
   14     5        NEW                                                  $19     'DOMDocument'
          6        DO_FCALL                                          0          
          7        ASSIGN                                                       !2, $19
   15     8        INIT_METHOD_CALL                                             !2, 'loadHTML'
          9        SEND_VAR_EX                                                  !0
         10        SEND_VAL_EX                                                  8196
         11        DO_FCALL                                          0          
   17    12        NEW                                                  $23     'DOMXPath'
         13        SEND_VAR_EX                                                  !2
         14        DO_FCALL                                          0          
         15        ASSIGN                                                       !3, $23
   19    16        ASSIGN                                                       !4, <array>
   20    17      > FE_RESET_R                                           $27     !1, ->27
         18    > > FE_FETCH_R                                           ~28     $27, !5, ->27
         19    >   ASSIGN                                                       !6, ~28
   21    20        INIT_FCALL                                                   'preg_quote'
         21        SEND_VAR                                                     !6
         22        SEND_VAL                                                     '%7E'
         23        DO_ICALL                                             $31     
         24        ASSIGN_DIM                                                   !4
         25        OP_DATA                                                      $31
   20    26      > JMP                                                          ->18
         27    >   FE_FREE                                                      $27
   23    28        FRAMELESS_ICALL_2                implode             ~32     '%7C', !4
         29        CONCAT                                               ~33     '%7E%5Cb%28', ~32
         30        CONCAT                                               ~34     ~33, '%29%5Cb%7Eiu'
         31        ASSIGN                                                       !7, ~34
   25    32        INIT_METHOD_CALL                                             !3, 'query'
         33        SEND_VAL_EX                                                  '%2F%2F%2A%5Bnot%28self%3A%3Aa%29%5D%2Ftext%28%29'
         34        DO_FCALL                                          0  $36     
         35      > FE_RESET_R                                           $37     $36, ->90
         36    > > FE_FETCH_R                                                   $37, !8, ->90
   26    37    >   ASSIGN                                                       !9, <array>
   27    38        ASSIGN                                                       !10, <false>
   28    39        INIT_FCALL                                                   'preg_split'
         40        SEND_VAR                                                     !7
         41        FETCH_OBJ_R                                          ~40     !8, 'nodeValue'
         42        SEND_VAL                                                     ~40
         43        SEND_VAL                                                     0
         44        SEND_VAL                                                     3
         45        DO_ICALL                                             $41     
         46      > FE_RESET_R                                           $42     $41, ->72
         47    > > FE_FETCH_R                                                   $42, !11, ->72
   29    48    >   INIT_FCALL                                                   'strtolower'
         49        SEND_VAR                                                     !11
         50        DO_ICALL                                             $43     
         51        ASSIGN                                                       !12, $43
   30    52        ISSET_ISEMPTY_DIM_OBJ                             0          !1, !12
         53      > JMPZ                                                         ~45, ->66
   31    54    >   ASSIGN                                                       !10, <true>
   32    55        INIT_METHOD_CALL                                             !2, 'createElement'
         56        CHECK_FUNC_ARG                                               
         57        FETCH_DIM_FUNC_ARG                                   $47     !1, !12
         58        SEND_FUNC_ARG                                                $47
         59        DO_FCALL                                          0  $48     
         60        ASSIGN                                                       !13, $48
   33    61        ASSIGN_OBJ                                                   !13, 'nodeValue'
         62        OP_DATA                                                      !11
   34    63        ASSIGN_DIM                                                   !9
         64        OP_DATA                                                      !13
   30    65      > JMP                                                          ->71
   36    66    >   INIT_METHOD_CALL                                             !2, 'createTextNode'
         67        SEND_VAR_EX                                                  !11
         68        DO_FCALL                                          0  $53     
         69        ASSIGN_DIM                                                   !9
         70        OP_DATA                                                      $53
   28    71    > > JMP                                                          ->47
         72    >   FE_FREE                                                      $42
   39    73      > JMPZ                                                         !10, ->89
   40    74    >   INIT_METHOD_CALL                                             !2, 'createDocumentFragment'
         75        DO_FCALL                                          0  $54     
         76        ASSIGN                                                       !14, $54
   41    77      > FE_RESET_R                                           $56     !9, ->83
         78    > > FE_FETCH_R                                                   $56, !15, ->83
   42    79    >   INIT_METHOD_CALL                                             !14, 'appendChild'
         80        SEND_VAR_EX                                                  !15
         81        DO_FCALL                                          0          
   41    82      > JMP                                                          ->78
         83    >   FE_FREE                                                      $56
   44    84        FETCH_OBJ_R                                          ~58     !8, 'parentNode'
         85        INIT_METHOD_CALL                                             ~58, 'replaceChild'
         86        SEND_VAR_EX                                                  !14
         87        SEND_VAR_EX                                                  !8
         88        DO_FCALL                                          0          
   25    89    > > JMP                                                          ->36
         90    >   FE_FREE                                                      $37
   47    91        INIT_FCALL                                                   'utf8_decode'
         92        INIT_METHOD_CALL                                             !2, 'saveHTML'
         93        CHECK_FUNC_ARG                                               
         94        FETCH_OBJ_FUNC_ARG                                   $60     !2, 'documentElement'
         95        SEND_FUNC_ARG                                                $60
         96        DO_FCALL                                          0  $61     
         97        SEND_VAR                                                     $61
         98        DO_FCALL_BY_NAME                                             
         99        FRAMELESS_ICALL_1                trim                ~63     $62
        100        FRAMELESS_ICALL_3                substr              ~64     ~63, 3
        101        OP_DATA                                                      -4
        102        ECHO                                                         ~64
        103      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
151.74 ms | 2166 KiB | 18 Q