3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = "<p>This paragraph has <b>bold text</b> &amp; <a href='#'>links with <b>bold text</b></a>.</p>"; // load into dom $doc = new DOMDocument(); $doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); function replaceOneTag($tag) { static $counter = 0; $counter++; return "<a href='#{$counter}'>{$tag}</a>"; } function replaceTags(array $tags, $node) { // skip links if ($node instanceof DOMElement && $node->hasAttribute("href")) { // skip } // process elements else if ($node instanceof DOMText) { // render back to html, which will deal with entities $oldhtml = $node->ownerDocument->saveHTML($node); // replace tags $newhtml = preg_replace_callback('#\b(' . implode('|', $tags) . ')\b#', function($match) { return replaceOneTag($match[0]); }, $oldhtml); // load the new html string into the document and replace the current DOMText node $newnode = $node->ownerDocument->createDocumentFragment(); $newnode->appendXML($newhtml); $node->parentNode->replaceChild($newnode, $node); } // handle other children else if ($node instanceof DOMNode && $node->hasChildNodes()) { // the child node list updates as the document is modified, which affects the foreach for ($child = $node->firstChild; $child; $child = $nextchild) { // get the next child node in advance $nextchild = $child->nextSibling; replaceTags($tags, $child); } } } print_r(replaceTags(["paragraph", "bold"], $doc)); echo $doc->saveHTML();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sehhK
function name:  (null)
number of ops:  19
compiled vars:  !0 = $html, !1 = $doc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cp%3EThis+paragraph+has+%3Cb%3Ebold+text%3C%2Fb%3E+%26amp%3B+%3Ca+href%3D%27%23%27%3Elinks+with+%3Cb%3Ebold+text%3C%2Fb%3E%3C%2Fa%3E.%3C%2Fp%3E'
    6     1        NEW                                              $3      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
    7     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAL_EX                                              8196
          7        DO_FCALL                                      0          
   49     8        INIT_FCALL                                               'print_r'
          9        INIT_FCALL                                               'replacetags'
         10        SEND_VAL                                                 <array>
         11        SEND_VAR                                                 !1
         12        DO_FCALL                                      0  $7      
         13        SEND_VAR                                                 $7
         14        DO_ICALL                                                 
   50    15        INIT_METHOD_CALL                                         !1, 'saveHTML'
         16        DO_FCALL                                      0  $9      
         17        ECHO                                                     $9
         18      > RETURN                                                   1

Function replaceonetag:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sehhK
function name:  replaceOneTag
number of ops:  10
compiled vars:  !0 = $tag, !1 = $counter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        BIND_STATIC                                              !1
   11     2        PRE_INC                                                  !1
   12     3        ROPE_INIT                                     5  ~4      '%3Ca+href%3D%27%23'
          4        ROPE_ADD                                      1  ~4      ~4, !1
          5        ROPE_ADD                                      2  ~4      ~4, '%27%3E'
          6        ROPE_ADD                                      3  ~4      ~4, !0
          7        ROPE_END                                      4  ~3      ~4, '%3C%2Fa%3E'
          8      > RETURN                                                   ~3
   13     9*     > RETURN                                                   null

End of function replaceonetag

Function replacetags:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 43
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 43
2 jumps found. (Code = 46) Position 1 = 45, Position 2 = 48
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 60
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 52
Branch analysis from position: 60
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 52
Branch analysis from position: 60
Branch analysis from position: 52
Branch analysis from position: 60
Branch analysis from position: 48
Branch analysis from position: 8
filename:       /in/sehhK
function name:  replaceTags
number of ops:  61
compiled vars:  !0 = $tags, !1 = $node, !2 = $oldhtml, !3 = $newhtml, !4 = $newnode, !5 = $child, !6 = $nextchild
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        INSTANCEOF                                       ~7      !1, 'DOMElement'
          3      > JMPZ_EX                                          ~7      ~7, ->8
          4    >   INIT_METHOD_CALL                                         !1, 'hasAttribute'
          5        SEND_VAL_EX                                              'href'
          6        DO_FCALL                                      0  $8      
          7        BOOL                                             ~7      $8
          8    > > JMPZ                                                     ~7, ->10
          9    > > JMP                                                      ->60
   22    10    >   INSTANCEOF                                               !1, 'DOMText'
         11      > JMPZ                                                     ~9, ->43
   24    12    >   FETCH_OBJ_R                                      ~10     !1, 'ownerDocument'
         13        INIT_METHOD_CALL                                         ~10, 'saveHTML'
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0  $11     
         16        ASSIGN                                                   !2, $11
   27    17        INIT_FCALL                                               'preg_replace_callback'
         18        INIT_FCALL                                               'implode'
         19        SEND_VAL                                                 '%7C'
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $13     
         22        CONCAT                                           ~14     '%23%5Cb%28', $13
         23        CONCAT                                           ~15     ~14, '%29%5Cb%23'
         24        SEND_VAL                                                 ~15
         25        DECLARE_LAMBDA_FUNCTION                          ~16     [0]
   29    26        SEND_VAL                                                 ~16
         27        SEND_VAR                                                 !2
   27    28        DO_ICALL                                         $17     
         29        ASSIGN                                                   !3, $17
   32    30        FETCH_OBJ_R                                      ~19     !1, 'ownerDocument'
         31        INIT_METHOD_CALL                                         ~19, 'createDocumentFragment'
         32        DO_FCALL                                      0  $20     
         33        ASSIGN                                                   !4, $20
   33    34        INIT_METHOD_CALL                                         !4, 'appendXML'
         35        SEND_VAR_EX                                              !3
         36        DO_FCALL                                      0          
   34    37        FETCH_OBJ_R                                      ~23     !1, 'parentNode'
         38        INIT_METHOD_CALL                                         ~23, 'replaceChild'
         39        SEND_VAR_EX                                              !4
         40        SEND_VAR_EX                                              !1
         41        DO_FCALL                                      0          
   22    42      > JMP                                                      ->60
   38    43    >   INSTANCEOF                                       ~25     !1, 'DOMNode'
         44      > JMPZ_EX                                          ~25     ~25, ->48
         45    >   INIT_METHOD_CALL                                         !1, 'hasChildNodes'
         46        DO_FCALL                                      0  $26     
         47        BOOL                                             ~25     $26
         48    > > JMPZ                                                     ~25, ->60
   40    49    >   FETCH_OBJ_R                                      ~27     !1, 'firstChild'
         50        ASSIGN                                                   !5, ~27
         51      > JMP                                                      ->59
   42    52    >   FETCH_OBJ_R                                      ~29     !5, 'nextSibling'
         53        ASSIGN                                                   !6, ~29
   44    54        INIT_FCALL_BY_NAME                                       'replaceTags'
         55        SEND_VAR_EX                                              !0
         56        SEND_VAR_EX                                              !5
         57        DO_FCALL                                      0          
   40    58        ASSIGN                                                   !5, !6
         59    > > JMPNZ                                                    !5, ->52
   47    60    > > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sehhK
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        INIT_FCALL                                               'replaceonetag'
          2        FETCH_DIM_R                                      ~1      !0, 0
          3        SEND_VAL                                                 ~1
          4        DO_FCALL                                      0  $2      
          5      > RETURN                                                   $2
   29     6*     > RETURN                                                   null

End of Dynamic Function 0

End of function replacetags

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
275.28 ms | 1024 KiB | 18 Q