3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * DOM xpath to find #text nodes and wrap in paragraph tag * @link http://stackoverflow.com/q/15552560/367456 */ $html = '<div> This text should be wrapped in a p tag. </div> This also should be wrapped. <b>And</b> this.'; $paragraphSequence = "\n\n"; $paragraphSequenceLength = 2; $paragraphComment = 'break'; /** * @param DOMNode $element * * @link http://php.net/book.dom * @return string */ $innerHTML = function (DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; }; /** * @param $html * * @return DOMElement * @throws InvalidArgumentException */ $loadHTMLFragment = function ($html) { $doc = new DOMDocument(); $doc->loadHTML("<body>$html"); $anchor = $doc->getElementsByTagName('body')->item(0); if (!$anchor) { throw new InvalidArgumentException('Unable to load HTML fragment.'); } return $anchor; }; $insertBreakMarkBefore = function (DOMText $node, $paragraphSequenceBefore = FALSE) use ($paragraphSequence, $paragraphComment) { if ($paragraphSequenceBefore) { $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); } $node->parentNode->insertBefore($node->ownerDocument->createComment($paragraphComment), $node); $node->parentNode->insertBefore($node->ownerDocument->createTextNode($paragraphSequence), $node); }; $anchor = $loadHTMLFragment($html); $xp = new DOMXPath($anchor->ownerDocument); /* @var $result DOMText[] */ $result = $xp->query('(.|./div)/text()', $anchor); foreach ($result as $i => $node) { if ($node->parentNode->tagName == 'div') { $insertBreakMarkBefore($node, true); } while (FALSE !== $pos = strpos($node->data, $paragraphSequence)) { $node = $node->splitText($pos + $paragraphSequenceLength); $insertBreakMarkBefore($node); } } $needle = sprintf('%1$s<!--%2$s-->%1$s', $paragraphSequence, $paragraphComment); $replace = sprintf("\n<p class=\"%s\">\n", $paragraphComment); $html = strtr($innerHTML($anchor), array($needle . $needle => $replace, $needle => $replace)); echo "HTML afterwards:\n", $innerHTML($loadHTMLFragment($html));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 56
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 56
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 39
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 39
Branch analysis from position: 55
Branch analysis from position: 39
Branch analysis from position: 38
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
filename:       /in/6fj1b
function name:  (null)
number of ops:  88
compiled vars:  !0 = $html, !1 = $paragraphSequence, !2 = $paragraphSequenceLength, !3 = $paragraphComment, !4 = $innerHTML, !5 = $loadHTMLFragment, !6 = $insertBreakMarkBefore, !7 = $anchor, !8 = $xp, !9 = $result, !10 = $node, !11 = $i, !12 = $pos, !13 = $needle, !14 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   ASSIGN                                                   !0, '%3Cdiv%3E%0A++++This+text+should+be+wrapped+in+a+p+tag.%0A%3C%2Fdiv%3E%0A%0AThis+also+should+be+wrapped.%0A%0A%3Cb%3EAnd%3C%2Fb%3E+this.'
   15     1        ASSIGN                                                   !1, '%0A%0A'
   16     2        ASSIGN                                                   !2, 2
   17     3        ASSIGN                                                   !3, 'break'
   25     4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6fj1b%3A25%240'
          5        ASSIGN                                                   !4, ~19
   43     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6fj1b%3A43%241'
          7        ASSIGN                                                   !5, ~21
   55     8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6fj1b%3A55%242'
          9        BIND_LEXICAL                                             ~23, !1
         10        BIND_LEXICAL                                             ~23, !3
         11        ASSIGN                                                   !6, ~23
   65    12        INIT_DYNAMIC_CALL                                        !5
         13        SEND_VAR_EX                                              !0
         14        DO_FCALL                                      0  $25     
         15        ASSIGN                                                   !7, $25
   66    16        NEW                                              $27     'DOMXPath'
         17        CHECK_FUNC_ARG                                           
         18        FETCH_OBJ_FUNC_ARG                               $28     !7, 'ownerDocument'
         19        SEND_FUNC_ARG                                            $28
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !8, $27
   69    22        INIT_METHOD_CALL                                         !8, 'query'
         23        SEND_VAL_EX                                              '%28.%7C.%2Fdiv%29%2Ftext%28%29'
         24        SEND_VAR_EX                                              !7
         25        DO_FCALL                                      0  $31     
         26        ASSIGN                                                   !9, $31
   71    27      > FE_RESET_R                                       $33     !9, ->56
         28    > > FE_FETCH_R                                       ~34     $33, !10, ->56
         29    >   ASSIGN                                                   !11, ~34
   73    30        FETCH_OBJ_R                                      ~36     !10, 'parentNode'
         31        FETCH_OBJ_R                                      ~37     ~36, 'tagName'
         32        IS_EQUAL                                                 ~37, 'div'
         33      > JMPZ                                                     ~38, ->38
   75    34    >   INIT_DYNAMIC_CALL                                        !6
         35        SEND_VAR_EX                                              !10
         36        SEND_VAL_EX                                              <true>
         37        DO_FCALL                                      0          
   78    38    > > JMP                                                      ->47
   80    39    >   INIT_METHOD_CALL                                         !10, 'splitText'
         40        ADD                                              ~40     !12, !2
         41        SEND_VAL_EX                                              ~40
         42        DO_FCALL                                      0  $41     
         43        ASSIGN                                                   !10, $41
   81    44        INIT_DYNAMIC_CALL                                        !6
         45        SEND_VAR_EX                                              !10
         46        DO_FCALL                                      0          
   78    47    >   INIT_FCALL                                               'strpos'
         48        FETCH_OBJ_R                                      ~44     !10, 'data'
         49        SEND_VAL                                                 ~44
         50        SEND_VAR                                                 !1
         51        DO_ICALL                                         $45     
         52        ASSIGN                                           ~46     !12, $45
         53        TYPE_CHECK                                  1018          ~46
         54      > JMPNZ                                                    ~47, ->39
   71    55    > > JMP                                                      ->28
         56    >   FE_FREE                                                  $33
   85    57        INIT_FCALL                                               'sprintf'
         58        SEND_VAL                                                 '%251%24s%3C%21--%252%24s--%3E%251%24s'
         59        SEND_VAR                                                 !1
         60        SEND_VAR                                                 !3
         61        DO_ICALL                                         $48     
         62        ASSIGN                                                   !13, $48
   86    63        INIT_FCALL                                               'sprintf'
         64        SEND_VAL                                                 '%0A%3Cp+class%3D%22%25s%22%3E%0A'
         65        SEND_VAR                                                 !3
         66        DO_ICALL                                         $50     
         67        ASSIGN                                                   !14, $50
   87    68        INIT_FCALL                                               'strtr'
         69        INIT_DYNAMIC_CALL                                        !4
         70        SEND_VAR_EX                                              !7
         71        DO_FCALL                                      0  $52     
         72        SEND_VAR                                                 $52
         73        CONCAT                                           ~53     !13, !13
         74        INIT_ARRAY                                       ~54     !14, ~53
         75        ADD_ARRAY_ELEMENT                                ~54     !14, !13
         76        SEND_VAL                                                 ~54
         77        DO_ICALL                                         $55     
         78        ASSIGN                                                   !0, $55
   89    79        ECHO                                                     'HTML+afterwards%3A%0A'
         80        INIT_DYNAMIC_CALL                                        !4
         81        INIT_DYNAMIC_CALL                                        !5
         82        SEND_VAR_EX                                              !0
         83        DO_FCALL                                      0  $57     
         84        SEND_VAR_NO_REF_EX                                       $57
         85        DO_FCALL                                      0  $58     
         86        ECHO                                                     $58
         87      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F6fj1b%3A25%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/6fj1b
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $element, !1 = $innerHTML, !2 = $children, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        ASSIGN                                                   !1, ''
   28     2        FETCH_OBJ_R                                      ~5      !0, 'childNodes'
          3        ASSIGN                                                   !2, ~5
   30     4      > FE_RESET_R                                       $7      !2, ->12
          5    > > FE_FETCH_R                                               $7, !3, ->12
   32     6    >   FETCH_OBJ_R                                      ~8      !0, 'ownerDocument'
          7        INIT_METHOD_CALL                                         ~8, 'saveHTML'
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $9      
         10        ASSIGN_OP                                     8          !1, $9
   30    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $7
   34    13      > RETURN                                                   !1
   35    14*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6fj1b%3A25%240

Function %00%7Bclosure%7D%2Fin%2F6fj1b%3A43%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 22
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6fj1b
function name:  {closure}
number of ops:  24
compiled vars:  !0 = $html, !1 = $doc, !2 = $anchor
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   45     1        NEW                                              $3      'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   46     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        NOP                                                      
          6        FAST_CONCAT                                      ~6      '%3Cbody%3E', !0
          7        SEND_VAL_EX                                              ~6
          8        DO_FCALL                                      0          
   47     9        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         10        SEND_VAL_EX                                              'body'
         11        DO_FCALL                                      0  $8      
         12        INIT_METHOD_CALL                                         $8, 'item'
         13        SEND_VAL_EX                                              0
         14        DO_FCALL                                      0  $9      
         15        ASSIGN                                                   !2, $9
   48    16        BOOL_NOT                                         ~11     !2
         17      > JMPZ                                                     ~11, ->22
   50    18    >   NEW                                              $12     'InvalidArgumentException'
         19        SEND_VAL_EX                                              'Unable+to+load+HTML+fragment.'
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $12
   52    22    > > RETURN                                                   !2
   53    23*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6fj1b%3A43%241

Function %00%7Bclosure%7D%2Fin%2F6fj1b%3A55%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/6fj1b
function name:  {closure}
number of ops:  33
compiled vars:  !0 = $node, !1 = $paragraphSequenceBefore, !2 = $paragraphSequence, !3 = $paragraphComment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   57     4      > JMPZ                                                     !1, ->14
   59     5    >   FETCH_OBJ_R                                      ~4      !0, 'parentNode'
          6        INIT_METHOD_CALL                                         ~4, 'insertBefore'
          7        FETCH_OBJ_R                                      ~5      !0, 'ownerDocument'
          8        INIT_METHOD_CALL                                         ~5, 'createTextNode'
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $6      
         11        SEND_VAR_NO_REF_EX                                       $6
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
   61    14    >   FETCH_OBJ_R                                      ~8      !0, 'parentNode'
         15        INIT_METHOD_CALL                                         ~8, 'insertBefore'
         16        FETCH_OBJ_R                                      ~9      !0, 'ownerDocument'
         17        INIT_METHOD_CALL                                         ~9, 'createComment'
         18        SEND_VAR_EX                                              !3
         19        DO_FCALL                                      0  $10     
         20        SEND_VAR_NO_REF_EX                                       $10
         21        SEND_VAR_EX                                              !0
         22        DO_FCALL                                      0          
   62    23        FETCH_OBJ_R                                      ~12     !0, 'parentNode'
         24        INIT_METHOD_CALL                                         ~12, 'insertBefore'
         25        FETCH_OBJ_R                                      ~13     !0, 'ownerDocument'
         26        INIT_METHOD_CALL                                         ~13, 'createTextNode'
         27        SEND_VAR_EX                                              !2
         28        DO_FCALL                                      0  $14     
         29        SEND_VAR_NO_REF_EX                                       $14
         30        SEND_VAR_EX                                              !0
         31        DO_FCALL                                      0          
   63    32      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6fj1b%3A55%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.89 ms | 1412 KiB | 19 Q