3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyParser { private $domdoc; public function __construct(DOMDocument $domdoc){ $this->domdoc = $domdoc; } public function __toString() { return $this->domdoc->saveHTML(); } public function replaceVariableInside($varName, $text, $idType, $idValue) { $nodes = $this->getNodes($idType, $idValue); $newTextNode = $this->getNodeFromText($text); array_map(function($node) use ($varName, $newTextNode){ /* @var $node DOMNode */ $textNodes = array_filter($this->dnl2array($node->childNodes), function($node){ return $node->nodeType === XML_TEXT_NODE; }); foreach ($textNodes as $textNode) { /* @var $textNode \DOMText */ $pos = strpos($textNode->nodeValue, $varName); if($pos !== false) { $newNode = $textNode->splitText($pos); $newNode->nodeValue = substr($newNode->nodeValue, strlen($varName)); $newNode->parentNode->insertBefore($newTextNode->cloneNode(true), $newNode); } } }, $nodes); } protected function getNodes($idType, $idValue) { switch ($idType){ case 'id': return [ $this->domdoc->getElementById($idValue) ]; break; case 'class': return $this->dnl2array((new DOMXPath($this->domdoc)) ->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $idValue ')]")); break; default: break; } } public function insertInto($idType, $id, $text) { $newnode = $this->getNodeFromText($text); array_map(function($elementOfDom) use ($newnode){ /* @var $elementOfDom DOMNode */ @$elementOfDom->appendChild($newnode); }, $this->getNodes($idType, $id)); } protected function getNodeFromText($text) { $text = html_entity_decode($text); try { $tmp = new DOMDocument; $textDocumentRes = @$tmp->loadHTML('<?xml encoding="utf-8" ?><span>'.$text.'</span>'); } catch(Exception $e) { $textDocumentRes = false; } if($textDocumentRes) { $newnode = $this->domdoc->importNode($tmp->documentElement->firstChild->firstChild, true); } else { $newnode = $this->domdoc->createTextNode($text); $newnode->nodeValue = $text; } return $newnode; } private function dnl2array($domnodelist) { $return = array(); $length = $domnodelist->length; for ($i = 0; $i < $length; ++$i) { $return[] = $domnodelist->item($i); } return $return; } } $domdocu = new DomDocument(); $domdocu->loadHTML(' <html> <head> </head> <body> <div id="first"> b%ELEMENT%a </div> <div class="second"> %ELEMENT% <div class="third"> %ELEMENT% </div> </div> <div class="fourth">%ELEMENT%</div> <div class="fourth">%ELEMENT%</div> <div class="fifth">a%ELEMENT%b</div> </body> </html> '); $parser = new MyParser($domdocu); $parser->insertInto('class', 'third', 'newtesttxtxttx'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'id', 'first'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'second'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'fourth'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'fifth'); echo $parser;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  (null)
number of ops:  42
compiled vars:  !0 = $domdocu, !1 = $parser
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'myparser'
  104     1        NEW                                              $2      'DomDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
  105     4        INIT_METHOD_CALL                                         !0, 'loadHTML'
          5        SEND_VAL_EX                                              '%0A%3Chtml%3E%0A%3Chead%3E%0A%3C%2Fhead%3E%0A%3Cbody%3E%0A++++%3Cdiv+id%3D%22first%22%3E%0A++++b%25ELEMENT%25a%0A++++%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22second%22%3E%0A++++++++%25ELEMENT%25%0A++++++++%3Cdiv+class%3D%22third%22%3E%0A++++++++%25ELEMENT%25%0A++++++++%3C%2Fdiv%3E%0A++++%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22fourth%22%3E%25ELEMENT%25%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22fourth%22%3E%25ELEMENT%25%3C%2Fdiv%3E%0A++++%3Cdiv+class%3D%22fifth%22%3Ea%25ELEMENT%25b%3C%2Fdiv%3E%0A%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A'
          6        DO_FCALL                                      0          
  125     7        NEW                                              $6      'MyParser'
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $6
  126    11        INIT_METHOD_CALL                                         !1, 'insertInto'
         12        SEND_VAL_EX                                              'class'
         13        SEND_VAL_EX                                              'third'
         14        SEND_VAL_EX                                              'newtesttxtxttx'
         15        DO_FCALL                                      0          
  127    16        INIT_METHOD_CALL                                         !1, 'replaceVariableInside'
         17        SEND_VAL_EX                                              '%25ELEMENT%25'
         18        SEND_VAL_EX                                              'TEEEXT'
         19        SEND_VAL_EX                                              'id'
         20        SEND_VAL_EX                                              'first'
         21        DO_FCALL                                      0          
  128    22        INIT_METHOD_CALL                                         !1, 'replaceVariableInside'
         23        SEND_VAL_EX                                              '%25ELEMENT%25'
         24        SEND_VAL_EX                                              'TEEEXT'
         25        SEND_VAL_EX                                              'class'
         26        SEND_VAL_EX                                              'second'
         27        DO_FCALL                                      0          
  129    28        INIT_METHOD_CALL                                         !1, 'replaceVariableInside'
         29        SEND_VAL_EX                                              '%25ELEMENT%25'
         30        SEND_VAL_EX                                              'TEEEXT'
         31        SEND_VAL_EX                                              'class'
         32        SEND_VAL_EX                                              'fourth'
         33        DO_FCALL                                      0          
  130    34        INIT_METHOD_CALL                                         !1, 'replaceVariableInside'
         35        SEND_VAL_EX                                              '%25ELEMENT%25'
         36        SEND_VAL_EX                                              'TEEEXT'
         37        SEND_VAL_EX                                              'class'
         38        SEND_VAL_EX                                              'fifth'
         39        DO_FCALL                                      0          
  132    40        ECHO                                                     !1
         41      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F1AFc4%3A22%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 46
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 46
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 45
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 45
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
filename:       /in/1AFc4
function name:  {closure}
number of ops:  48
compiled vars:  !0 = $node, !1 = $varName, !2 = $newTextNode, !3 = $textNodes, !4 = $textNode, !5 = $pos, !6 = $newNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   24     3        INIT_FCALL                                               'array_filter'
          4        FETCH_THIS                                       $7      
          5        INIT_METHOD_CALL                                         $7, 'dnl2array'
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $8      !0, 'childNodes'
          8        SEND_FUNC_ARG                                            $8
          9        DO_FCALL                                      0  $9      
         10        SEND_VAR                                                 $9
         11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1AFc4%3A24%241'
   26    12        SEND_VAL                                                 ~10
         13        DO_ICALL                                         $11     
   24    14        ASSIGN                                                   !3, $11
   27    15      > FE_RESET_R                                       $13     !3, ->46
         16    > > FE_FETCH_R                                               $13, !4, ->46
   29    17    >   INIT_FCALL                                               'strpos'
         18        FETCH_OBJ_R                                      ~14     !4, 'nodeValue'
         19        SEND_VAL                                                 ~14
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !5, $15
   30    23        TYPE_CHECK                                  1018          !5
         24      > JMPZ                                                     ~17, ->45
   32    25    >   INIT_METHOD_CALL                                         !4, 'splitText'
         26        SEND_VAR_EX                                              !5
         27        DO_FCALL                                      0  $18     
         28        ASSIGN                                                   !6, $18
   33    29        INIT_FCALL                                               'substr'
         30        FETCH_OBJ_R                                      ~21     !6, 'nodeValue'
         31        SEND_VAL                                                 ~21
         32        STRLEN                                           ~22     !1
         33        SEND_VAL                                                 ~22
         34        DO_ICALL                                         $23     
         35        ASSIGN_OBJ                                               !6, 'nodeValue'
         36        OP_DATA                                                  $23
   35    37        FETCH_OBJ_R                                      ~24     !6, 'parentNode'
         38        INIT_METHOD_CALL                                         ~24, 'insertBefore'
         39        INIT_METHOD_CALL                                         !2, 'cloneNode'
         40        SEND_VAL_EX                                              <true>
         41        DO_FCALL                                      0  $25     
         42        SEND_VAR_NO_REF_EX                                       $25
         43        SEND_VAR_EX                                              !6
         44        DO_FCALL                                      0          
   27    45    > > JMP                                                      ->16
         46    >   FE_FREE                                                  $13
   38    47      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1AFc4%3A22%240

Function %00%7Bclosure%7D%2Fin%2F1AFc4%3A24%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        FETCH_OBJ_R                                      ~1      !0, 'nodeType'
          2        IS_IDENTICAL                                     ~2      ~1, 3
          3      > RETURN                                                   ~2
   26     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1AFc4%3A24%241

Function %00%7Bclosure%7D%2Fin%2F1AFc4%3A62%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $elementOfDom, !1 = $newnode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   64     2        BEGIN_SILENCE                                    ~2      
          3        INIT_METHOD_CALL                                         !0, 'appendChild'
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
          6        END_SILENCE                                              ~2
   65     7      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F1AFc4%3A62%242

Class MyParser:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  __construct
number of ops:  4
compiled vars:  !0 = $domdoc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1        ASSIGN_OBJ                                               'domdoc'
          2        OP_DATA                                                  !0
    8     3      > RETURN                                                   null

End of function __construct

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  __toString
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   FETCH_OBJ_R                                      ~0      'domdoc'
          1        INIT_METHOD_CALL                                         ~0, 'saveHTML'
          2        DO_FCALL                                      0  $1      
          3        VERIFY_RETURN_TYPE                                       $1
          4      > RETURN                                                   $1
   13     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function __tostring

Function replacevariableinside:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  replaceVariableInside
number of ops:  21
compiled vars:  !0 = $varName, !1 = $text, !2 = $idType, !3 = $idValue, !4 = $nodes, !5 = $newTextNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   18     4        INIT_METHOD_CALL                                         'getNodes'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !3
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !4, $6
   20     9        INIT_METHOD_CALL                                         'getNodeFromText'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $8      
         12        ASSIGN                                                   !5, $8
   22    13        INIT_FCALL                                               'array_map'
         14        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1AFc4%3A22%240'
         15        BIND_LEXICAL                                             ~10, !0
         16        BIND_LEXICAL                                             ~10, !5
   38    17        SEND_VAL                                                 ~10
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                                 
   39    20      > RETURN                                                   null

End of function replacevariableinside

Function getnodes:
Finding entry points
Branch analysis from position: 0
4 jumps found. (Code = 188) Position 1 = 8, Position 2 = 15, Position 3 = 31, Position 4 = 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 15
Branch analysis from position: 8
filename:       /in/1AFc4
function name:  getNodes
number of ops:  33
compiled vars:  !0 = $idType, !1 = $idValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   43     2      > SWITCH_STRING                                            !0, [ 'id':->8, 'class':->15, ], ->31
   44     3    >   IS_EQUAL                                                 !0, 'id'
          4      > JMPNZ                                                    ~2, ->8
   49     5    >   IS_EQUAL                                                 !0, 'class'
          6      > JMPNZ                                                    ~2, ->15
          7    > > JMP                                                      ->31
   46     8    >   FETCH_OBJ_R                                      ~3      'domdoc'
          9        INIT_METHOD_CALL                                         ~3, 'getElementById'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $4      
         12        INIT_ARRAY                                       ~5      $4
         13      > RETURN                                                   ~5
   48    14*       JMP                                                      ->32
   50    15    >   INIT_METHOD_CALL                                         'dnl2array'
         16        NEW                                              $6      'DOMXPath'
         17        CHECK_FUNC_ARG                                           
         18        FETCH_OBJ_FUNC_ARG                               $7      'domdoc'
         19        SEND_FUNC_ARG                                            $7
         20        DO_FCALL                                      0          
   51    21        INIT_METHOD_CALL                                         $6, 'query'
         22        ROPE_INIT                                     3  ~10     '%2F%2F%2A%5Bcontains%28concat%28%27+%27%2C+normalize-space%28%40class%29%2C+%27+%27%29%2C+%27+'
         23        ROPE_ADD                                      1  ~10     ~10, !1
         24        ROPE_END                                      2  ~9      ~10, '+%27%29%5D'
         25        SEND_VAL_EX                                              ~9
         26        DO_FCALL                                      0  $12     
         27        SEND_VAR_NO_REF_EX                                       $12
         28        DO_FCALL                                      0  $13     
         29      > RETURN                                                   $13
   52    30*       JMP                                                      ->32
   54    31    > > JMP                                                      ->32
   56    32    > > RETURN                                                   null

End of function getnodes

Function insertinto:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1AFc4
function name:  insertInto
number of ops:  18
compiled vars:  !0 = $idType, !1 = $id, !2 = $text, !3 = $newnode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   60     3        INIT_METHOD_CALL                                         'getNodeFromText'
          4        SEND_VAR_EX                                              !2
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !3, $4
   62     7        INIT_FCALL                                               'array_map'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F1AFc4%3A62%242'
          9        BIND_LEXICAL                                             ~6, !3
   65    10        SEND_VAL                                                 ~6
         11        INIT_METHOD_CALL                                         'getNodes'
         12        SEND_VAR_EX                                              !0
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $7      
         15        SEND_VAR                                                 $7
         16        DO_ICALL                                                 
   66    17      > RETURN                                                   null

End of function insertinto

Function getnodefromtext:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 31
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 17
Branch analysis from position: 17
2 jumps found. (Code = 107) Position 1 = 18, Position 2 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 31
Branch analysis from position: 20
Branch analysis from position: 31
filename:       /in/1AFc4
function name:  getNodeFromText
number of ops:  40
compiled vars:  !0 = $text, !1 = $tmp, !2 = $textDocumentRes, !3 = $e, !4 = $newnode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
   70     1        INIT_FCALL                                               'html_entity_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !0, $5
   73     5        NEW                                              $7      'DOMDocument'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $7
   74     8        BEGIN_SILENCE                                    ~10     
          9        INIT_METHOD_CALL                                         !1, 'loadHTML'
         10        CONCAT                                           ~11     '%3C%3Fxml+encoding%3D%22utf-8%22+%3F%3E%3Cspan%3E', !0
         11        CONCAT                                           ~12     ~11, '%3C%2Fspan%3E'
         12        SEND_VAL_EX                                              ~12
         13        DO_FCALL                                      0  $13     
         14        END_SILENCE                                              ~10
         15        ASSIGN                                                   !2, $13
         16      > JMP                                                      ->19
   76    17  E > > CATCH                                       last         'Exception'
   78    18    >   ASSIGN                                                   !2, <false>
   80    19    > > JMPZ                                                     !2, ->31
   82    20    >   FETCH_OBJ_R                                      ~16     'domdoc'
         21        INIT_METHOD_CALL                                         ~16, 'importNode'
         22        CHECK_FUNC_ARG                                           
         23        FETCH_OBJ_FUNC_ARG                               $17     !1, 'documentElement'
         24        FETCH_OBJ_FUNC_ARG                               $18     $17, 'firstChild'
         25        FETCH_OBJ_FUNC_ARG                               $19     $18, 'firstChild'
         26        SEND_FUNC_ARG                                            $19
         27        SEND_VAL_EX                                              <true>
         28        DO_FCALL                                      0  $20     
         29        ASSIGN                                                   !4, $20
         30      > JMP                                                      ->38
   86    31    >   FETCH_OBJ_R                                      ~22     'domdoc'
         32        INIT_METHOD_CALL                                         ~22, 'createTextNode'
         33        SEND_VAR_EX                                              !0
         34        DO_FCALL                                      0  $23     
         35        ASSIGN                                                   !4, $23
   87    36        ASSIGN_OBJ                                               !4, 'nodeValue'
         37        OP_DATA                                                  !0
   90    38    > > RETURN                                                   !4
   91    39*     > RETURN                                                   null

End of function getnodefromtext

Function dnl2array:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
Branch analysis from position: 6
filename:       /in/1AFc4
function name:  dnl2array
number of ops:  16
compiled vars:  !0 = $domnodelist, !1 = $return, !2 = $length, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   RECV                                             !0      
   95     1        ASSIGN                                                   !1, <array>
   96     2        FETCH_OBJ_R                                      ~5      !0, 'length'
          3        ASSIGN                                                   !2, ~5
   97     4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->12
   98     6    >   INIT_METHOD_CALL                                         !0, 'item'
          7        SEND_VAR_EX                                              !3
          8        DO_FCALL                                      0  $9      
          9        ASSIGN_DIM                                               !1
         10        OP_DATA                                                  $9
   97    11        PRE_INC                                                  !3
         12    >   IS_SMALLER                                               !3, !2
         13      > JMPNZ                                                    ~11, ->6
  100    14    > > RETURN                                                   !1
  101    15*     > RETURN                                                   null

End of function dnl2array

End of class MyParser.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.55 ms | 1412 KiB | 23 Q