3v4l.org

run code in 300+ 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 = 39, Position 2 = 93
Branch analysis from position: 39
2 jumps found. (Code = 78) Position 1 = 40, Position 2 = 93
Branch analysis from position: 40
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 75
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 75
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 69
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 92
Branch analysis from position: 77
2 jumps found. (Code = 77) Position 1 = 81, Position 2 = 86
Branch analysis from position: 81
2 jumps found. (Code = 78) Position 1 = 82, Position 2 = 86
Branch analysis from position: 82
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 86
Branch analysis from position: 92
Branch analysis from position: 75
Branch analysis from position: 93
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 93
Branch analysis from position: 27
filename:       /in/XXNkq
function name:  (null)
number of ops:  112
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        INIT_FCALL                                               'implode'
         29        SEND_VAL                                                 '%7C'
         30        SEND_VAR                                                 !4
         31        DO_ICALL                                         $32     
         32        CONCAT                                           ~33     '%7E%5Cb%28', $32
         33        CONCAT                                           ~34     ~33, '%29%5Cb%7Eiu'
         34        ASSIGN                                                   !7, ~34
   25    35        INIT_METHOD_CALL                                         !3, 'query'
         36        SEND_VAL_EX                                              '%2F%2F%2A%5Bnot%28self%3A%3Aa%29%5D%2Ftext%28%29'
         37        DO_FCALL                                      0  $36     
         38      > FE_RESET_R                                       $37     $36, ->93
         39    > > FE_FETCH_R                                               $37, !8, ->93
   26    40    >   ASSIGN                                                   !9, <array>
   27    41        ASSIGN                                                   !10, <false>
   28    42        INIT_FCALL                                               'preg_split'
         43        SEND_VAR                                                 !7
         44        FETCH_OBJ_R                                      ~40     !8, 'nodeValue'
         45        SEND_VAL                                                 ~40
         46        SEND_VAL                                                 0
         47        SEND_VAL                                                 3
         48        DO_ICALL                                         $41     
         49      > FE_RESET_R                                       $42     $41, ->75
         50    > > FE_FETCH_R                                               $42, !11, ->75
   29    51    >   INIT_FCALL                                               'strtolower'
         52        SEND_VAR                                                 !11
         53        DO_ICALL                                         $43     
         54        ASSIGN                                                   !12, $43
   30    55        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !12
         56      > JMPZ                                                     ~45, ->69
   31    57    >   ASSIGN                                                   !10, <true>
   32    58        INIT_METHOD_CALL                                         !2, 'createElement'
         59        CHECK_FUNC_ARG                                           
         60        FETCH_DIM_FUNC_ARG                               $47     !1, !12
         61        SEND_FUNC_ARG                                            $47
         62        DO_FCALL                                      0  $48     
         63        ASSIGN                                                   !13, $48
   33    64        ASSIGN_OBJ                                               !13, 'nodeValue'
         65        OP_DATA                                                  !11
   34    66        ASSIGN_DIM                                               !9
         67        OP_DATA                                                  !13
         68      > JMP                                                      ->74
   36    69    >   INIT_METHOD_CALL                                         !2, 'createTextNode'
         70        SEND_VAR_EX                                              !11
         71        DO_FCALL                                      0  $53     
         72        ASSIGN_DIM                                               !9
         73        OP_DATA                                                  $53
   28    74    > > JMP                                                      ->50
         75    >   FE_FREE                                                  $42
   39    76      > JMPZ                                                     !10, ->92
   40    77    >   INIT_METHOD_CALL                                         !2, 'createDocumentFragment'
         78        DO_FCALL                                      0  $54     
         79        ASSIGN                                                   !14, $54
   41    80      > FE_RESET_R                                       $56     !9, ->86
         81    > > FE_FETCH_R                                               $56, !15, ->86
   42    82    >   INIT_METHOD_CALL                                         !14, 'appendChild'
         83        SEND_VAR_EX                                              !15
         84        DO_FCALL                                      0          
   41    85      > JMP                                                      ->81
         86    >   FE_FREE                                                  $56
   44    87        FETCH_OBJ_R                                      ~58     !8, 'parentNode'
         88        INIT_METHOD_CALL                                         ~58, 'replaceChild'
         89        SEND_VAR_EX                                              !14
         90        SEND_VAR_EX                                              !8
         91        DO_FCALL                                      0          
   25    92    > > JMP                                                      ->39
         93    >   FE_FREE                                                  $37
   47    94        INIT_FCALL                                               'substr'
         95        INIT_FCALL                                               'trim'
         96        INIT_FCALL                                               'utf8_decode'
         97        INIT_METHOD_CALL                                         !2, 'saveHTML'
         98        CHECK_FUNC_ARG                                           
         99        FETCH_OBJ_FUNC_ARG                               $60     !2, 'documentElement'
        100        SEND_FUNC_ARG                                            $60
        101        DO_FCALL                                      0  $61     
        102        SEND_VAR                                                 $61
        103        DO_ICALL                                         $62     
        104        SEND_VAR                                                 $62
        105        DO_ICALL                                         $63     
        106        SEND_VAR                                                 $63
        107        SEND_VAL                                                 3
        108        SEND_VAL                                                 -4
        109        DO_ICALL                                         $64     
        110        ECHO                                                     $64
        111      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
224.78 ms | 1404 KiB | 29 Q