3v4l.org

run code in 300+ PHP versions simultaneously
<?php $post_content = "<p>There are 14 more days until our <a href=\"/somepage.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"join us\">holiday special</a> so come join us!</p>"; $dom = new DOMDocument(); $dom->loadHTML($post_content); $xpath = new DOMXPath($dom); $all_text_nodes = $xpath->query("//text()"); $words_left = 9; // reduced for this text to cut off at "holiday" foreach( $all_text_nodes as $text_node) { $text = $text_node->textContent; $words = explode(" ", $text); // TODO: maybe preg_split on /\s/ to support more whitespace types $word_count = count($words); if( $word_count < $words_left) { $words_left -= $word_count; continue; } // reached the threshold $words_that_fit = implode(" ", array_slice($words, 0, $words_left)); // If the above TODO is implemented, this will need to be adjusted to keep the specific whitespace characters $text_node->textContent = $words_that_fit; $remove_after = $text_node; while( $remove_after->parentNode) { while( $remove_after->nextSibling) { $remove_after->parentNode->removeChild($remove_after->nextSibling); } $remove_after = $remove_after->parentNode; } break; } $output = substr($dom->saveHTML($dom->getElementsByTagName("body")->item(0)), strlen("<body>"), -strlen("</body>")); echo $output;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 60
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 60
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 45
Branch analysis from position: 58
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: 45
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 46
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 45
Branch analysis from position: 58
Branch analysis from position: 45
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 46
Branch analysis from position: 54
Branch analysis from position: 46
Branch analysis from position: 60
Branch analysis from position: 60
filename:       /in/dOtqM
function name:  (null)
number of ops:  78
compiled vars:  !0 = $post_content, !1 = $dom, !2 = $xpath, !3 = $all_text_nodes, !4 = $words_left, !5 = $text_node, !6 = $text, !7 = $words, !8 = $word_count, !9 = $words_that_fit, !10 = $remove_after, !11 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cp%3EThere+are+14+more+days+until+our+%3Ca+href%3D%22%2Fsomepage.html%22+target%3D%22_blank%22+rel%3D%22noreferrer+noopener%22+aria-label%3D%22join+us%22%3Eholiday+special%3C%2Fa%3E+so+come+join+us%21%3C%2Fp%3E'
    5     1        NEW                                              $13     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $13
    6     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
    7     7        NEW                                              $17     'DOMXPath'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !2, $17
    8    11        INIT_METHOD_CALL                                         !2, 'query'
         12        SEND_VAL_EX                                              '%2F%2Ftext%28%29'
         13        DO_FCALL                                      0  $20     
         14        ASSIGN                                                   !3, $20
    9    15        ASSIGN                                                   !4, 9
   10    16      > FE_RESET_R                                       $23     !3, ->60
         17    > > FE_FETCH_R                                               $23, !5, ->60
   11    18    >   FETCH_OBJ_R                                      ~24     !5, 'textContent'
         19        ASSIGN                                                   !6, ~24
   12    20        INIT_FCALL                                               'explode'
         21        SEND_VAL                                                 '+'
         22        SEND_VAR                                                 !6
         23        DO_ICALL                                         $26     
         24        ASSIGN                                                   !7, $26
   13    25        COUNT                                            ~28     !7
         26        ASSIGN                                                   !8, ~28
   14    27        IS_SMALLER                                               !8, !4
         28      > JMPZ                                                     ~30, ->31
   15    29    >   ASSIGN_OP                                     2          !4, !8
   16    30      > JMP                                                      ->17
   19    31    >   INIT_FCALL                                               'implode'
         32        SEND_VAL                                                 '+'
         33        INIT_FCALL                                               'array_slice'
         34        SEND_VAR                                                 !7
         35        SEND_VAL                                                 0
         36        SEND_VAR                                                 !4
         37        DO_ICALL                                         $32     
         38        SEND_VAR                                                 $32
         39        DO_ICALL                                         $33     
         40        ASSIGN                                                   !9, $33
   21    41        ASSIGN_OBJ                                               !5, 'textContent'
         42        OP_DATA                                                  !9
   23    43        ASSIGN                                                   !10, !5
   24    44      > JMP                                                      ->56
   25    45    > > JMP                                                      ->52
   26    46    >   FETCH_OBJ_R                                      ~37     !10, 'parentNode'
         47        INIT_METHOD_CALL                                         ~37, 'removeChild'
         48        CHECK_FUNC_ARG                                           
         49        FETCH_OBJ_FUNC_ARG                               $38     !10, 'nextSibling'
         50        SEND_FUNC_ARG                                            $38
         51        DO_FCALL                                      0          
   25    52    >   FETCH_OBJ_R                                      ~40     !10, 'nextSibling'
         53      > JMPNZ                                                    ~40, ->46
   28    54    >   FETCH_OBJ_R                                      ~41     !10, 'parentNode'
         55        ASSIGN                                                   !10, ~41
   24    56    >   FETCH_OBJ_R                                      ~43     !10, 'parentNode'
         57      > JMPNZ                                                    ~43, ->45
   30    58    > > JMP                                                      ->60
   10    59*       JMP                                                      ->17
         60    >   FE_FREE                                                  $23
   32    61        INIT_FCALL                                               'substr'
         62        INIT_METHOD_CALL                                         !1, 'saveHTML'
         63        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
         64        SEND_VAL_EX                                              'body'
         65        DO_FCALL                                      0  $44     
         66        INIT_METHOD_CALL                                         $44, 'item'
         67        SEND_VAL_EX                                              0
         68        DO_FCALL                                      0  $45     
         69        SEND_VAR_NO_REF_EX                                       $45
         70        DO_FCALL                                      0  $46     
         71        SEND_VAR                                                 $46
         72        SEND_VAL                                                 6
         73        SEND_VAL                                                 -7
         74        DO_ICALL                                         $47     
         75        ASSIGN                                                   !11, $47
   34    76        ECHO                                                     !11
   35    77      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.67 ms | 1449 KiB | 17 Q