3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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)); var_dump($newTextNode); $textNode->nextSibling->insertBefore($newTextNode); } } }, $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> </body> </html> '); $parser = new MyParser($domdocu); $parser->insertInto('class', 'third', 'newtesttxtxttx'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'id', 'first'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'second'); echo $parser;
based on 6eF27
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
function name:  (null)
number of ops:  30
compiled vars:  !0 = $domdocu, !1 = $parser
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   3     0  E >   NOP                                                      
 104     1        NEW                                              $3      :-5
         2        DO_FCALL                                      0          
         3        ASSIGN                                                   !0, $3
 105     4        INIT_METHOD_CALL                                         !0, 'loadHTML'
 124     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%0A++++%25ELEMENT%25%0A++++%3C%2Fdiv%3E%0A%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A'
         6        DO_FCALL                                      0          
 125     7        NEW                                              $7      :0
         8        SEND_VAR_EX                                              !0
         9        DO_FCALL                                      0          
        10        ASSIGN                                                   !1, $7
 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          
 130    28        ECHO                                                     !1
        29      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FNkVAE0x7f45db6364a3:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 77) Position 1 = 14, Position 2 = 43
Branch analysis from position: 14
Jump found. (Code = 78) Position 1 = 15, Position 2 = 43
Branch analysis from position: 15
Jump found. (Code = 43) Position 1 = 23, Position 2 = 42
Branch analysis from position: 23
Jump found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 42
Branch analysis from position: 43
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
filename:       /in/NkVAE
function name:  {closure}
number of ops:  45
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, 'varName'
         2        BIND_STATIC                                              !2, 'newTextNode'
  24     3        INIT_FCALL                                               'array_filter'
         4        INIT_METHOD_CALL                                         'dnl2array'
         5        FETCH_OBJ_FUNC_ARG                               $7      !0, 'childNodes'
         6        SEND_VAR_EX                                              $7
         7        DO_FCALL                                      0  $8      
         8        SEND_VAR                                                 $8
         9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FNkVAE0x7f45db6362b5'
  26    10        SEND_VAL                                                 ~9
        11        DO_ICALL                                         $10     
        12        ASSIGN                                                   !3, $10
  27    13      > FE_RESET_R                                       $12     !3, ->43
        14    > > FE_FETCH_R                                               $12, !4, ->43
  29    15    >   INIT_FCALL                                               'strpos'
        16        FETCH_OBJ_R                                      $13     !4, 'nodeValue'
        17        SEND_VAR                                                 $13
        18        SEND_VAR                                                 !1
        19        DO_ICALL                                         $14     
        20        ASSIGN                                                   !5, $14
  30    21        IS_NOT_IDENTICAL                                 ~16     !5, <false>
        22      > JMPZ                                                     ~16, ->42
  32    23    >   INIT_METHOD_CALL                                         !4, 'splitText'
        24        SEND_VAR_EX                                              !5
        25        DO_FCALL                                      0  $17     
        26        ASSIGN                                                   !6, $17
  33    27        INIT_FCALL                                               'substr'
        28        FETCH_OBJ_R                                      $20     !6, 'nodeValue'
        29        SEND_VAR                                                 $20
        30        STRLEN                                           ~21     !1
        31        SEND_VAL                                                 ~21
        32        DO_ICALL                                         $22     
        33        ASSIGN_OBJ                                               !6, 'nodeValue'
        34        OP_DATA                                                  $22
  34    35        INIT_FCALL                                               'var_dump'
        36        SEND_VAR                                                 !2
        37        DO_ICALL                                                 
  35    38        FETCH_OBJ_R                                      $24     !4, 'nextSibling'
        39        INIT_METHOD_CALL                                         $24, 'insertBefore'
        40        SEND_VAR_EX                                              !2
        41        DO_FCALL                                      0          
        42    > > JMP                                                      ->14
        43    >   FE_FREE                                                  $12
  38    44      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FNkVAE0x7f45db6364a3

Function %00%7Bclosure%7D%2Fin%2FNkVAE0x7f45db6362b5:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
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%2FNkVAE0x7f45db6362b5

Function %00%7Bclosure%7D%2Fin%2FNkVAE0x7f45db6367ca:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
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, 'newnode'
  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%2FNkVAE0x7f45db6367ca

Class MyParser:
Function __construct:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
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
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
function name:  __toString
number of ops:  5
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      > RETURN                                                   $1
  13     4*     > RETURN                                                   null

End of function __tostring

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

End of function getnodes

Function insertinto:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/NkVAE
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%2FNkVAE0x7f45db6367ca'
         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
Jump found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Jump found. (Code = 43) Position 1 = 20, Position 2 = 30
Branch analysis from position: 20
Jump found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Jump found. (Code = 62) Position 1 = -2
Found catch point at position: 17
Branch analysis from position: 17
Jump found. (Code = 107) Position 1 = 18, Position 2 = -2
Branch analysis from position: 18
Jump found. (Code = 43) Position 1 = 20, Position 2 = 30
Branch analysis from position: 20
Branch analysis from position: 30
filename:       /in/NkVAE
function name:  getNodeFromText
number of ops:  39
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      :-4
         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                                                    'Exception', !3
  78    18    >   ASSIGN                                                   !2, <false>
  80    19    > > JMPZ                                                     !2, ->30
  82    20    >   FETCH_OBJ_R                                      $16     'domdoc'
        21        INIT_METHOD_CALL                                         $16, 'importNode'
        22        FETCH_OBJ_FUNC_ARG                               $17     !1, 'documentElement'
        23        FETCH_OBJ_FUNC_ARG                               $18     $17, 'firstChild'
        24        FETCH_OBJ_FUNC_ARG                               $19     $18, 'firstChild'
        25        SEND_VAR_EX                                              $19
        26        SEND_VAL_EX                                              <true>
        27        DO_FCALL                                      0  $20     
        28        ASSIGN                                                   !4, $20
        29      > JMP                                                      ->37
  86    30    >   FETCH_OBJ_R                                      $22     'domdoc'
        31        INIT_METHOD_CALL                                         $22, 'createTextNode'
        32        SEND_VAR_EX                                              !0
        33        DO_FCALL                                      0  $23     
        34        ASSIGN                                                   !4, $23
  87    35        ASSIGN_OBJ                                               !4, 'nodeValue'
        36        OP_DATA                                                  !0
  90    37    > > RETURN                                                   !4
  91    38*     > RETURN                                                   null

End of function getnodefromtext

Function dnl2array:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Jump found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
Jump found. (Code = 44) Position 1 = 14, Position 2 = 6
Branch analysis from position: 14
Branch analysis from position: 6
filename:       /in/NkVAE
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                                       ~11     !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 7.3.0