3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = '<div id="one-id" class="someClassName">Some text <a href="#" title="Words" id="linkId" class="classLink">link</a> with only the class and id attrtibutes.</div>'; $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); foreach ($xpath->query('//*') as $node) { for ($i = $node->attributes->length - 1; $i >= 0; --$i) { $attr = $node->attributes->item($i); if (!in_array($attr->name, ['id', 'class'])) { $node->removeAttribute($attr->name); } } } echo $dom->saveHTML();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 40
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 40
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 22
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 22
Branch analysis from position: 39
Branch analysis from position: 22
Branch analysis from position: 36
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/8VdRc
function name:  (null)
number of ops:  45
compiled vars:  !0 = $html, !1 = $dom, !2 = $xpath, !3 = $node, !4 = $i, !5 = $attr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cdiv+id%3D%22one-id%22+class%3D%22someClassName%22%3ESome+text+%3Ca+href%3D%22%23%22+title%3D%22Words%22+id%3D%22linkId%22+class%3D%22classLink%22%3Elink%3C%2Fa%3E+with+only+the+class+and+id++attrtibutes.%3C%2Fdiv%3E'
    5     1        NEW                                              $7      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $7
    6     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAL_EX                                              8196
          7        DO_FCALL                                      0          
    7     8        NEW                                              $11     'DOMXPath'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $11
    8    12        INIT_METHOD_CALL                                         !2, 'query'
         13        SEND_VAL_EX                                              '%2F%2F%2A'
         14        DO_FCALL                                      0  $14     
         15      > FE_RESET_R                                       $15     $14, ->40
         16    > > FE_FETCH_R                                               $15, !3, ->40
    9    17    >   FETCH_OBJ_R                                      ~16     !3, 'attributes'
         18        FETCH_OBJ_R                                      ~17     ~16, 'length'
         19        SUB                                              ~18     ~17, 1
         20        ASSIGN                                                   !4, ~18
         21      > JMP                                                      ->37
   10    22    >   FETCH_OBJ_R                                      ~20     !3, 'attributes'
         23        INIT_METHOD_CALL                                         ~20, 'item'
         24        SEND_VAR_EX                                              !4
         25        DO_FCALL                                      0  $21     
         26        ASSIGN                                                   !5, $21
   11    27        FETCH_OBJ_R                                      ~23     !5, 'name'
         28        IN_ARRAY                                         ~24     ~23, <array>
         29        BOOL_NOT                                         ~25     ~24
         30      > JMPZ                                                     ~25, ->36
   12    31    >   INIT_METHOD_CALL                                         !3, 'removeAttribute'
         32        CHECK_FUNC_ARG                                           
         33        FETCH_OBJ_FUNC_ARG                               $26     !5, 'name'
         34        SEND_FUNC_ARG                                            $26
         35        DO_FCALL                                      0          
    9    36    >   PRE_DEC                                                  !4
         37    >   IS_SMALLER_OR_EQUAL                                      0, !4
         38      > JMPNZ                                                    ~29, ->22
    8    39    > > JMP                                                      ->16
         40    >   FE_FREE                                                  $15
   16    41        INIT_METHOD_CALL                                         !1, 'saveHTML'
         42        DO_FCALL                                      0  $30     
         43        ECHO                                                     $30
         44      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
202.86 ms | 1007 KiB | 13 Q