3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = '<p>random</p> <a href="">Test 1</a> (target1) <br> <a href="">Test 2</a> (target2) <br> <a href="">Test 3</a> (skip) // etc '; $array = array( '(target1)', '(target2)' ); $contains = implode(" or ", array_map(function($x) { return "contains(., '$x')"; }, $array)); $doc = new DOMDocument(); $doc->loadHTML('<?xml encoding="utf-8" ?>' . $html); $xpath = new DOMXPath($doc); $elements = $xpath->query("//a/following-sibling::text()[$contains]"); $results = []; foreach ($elements as $element) { $results[] = [$element->previousSibling->nodeValue, trim($element->nodeValue)]; } print_r($results);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 44
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 44
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/7BiUN
function name:  (null)
number of ops:  49
compiled vars:  !0 = $html, !1 = $array, !2 = $contains, !3 = $doc, !4 = $xpath, !5 = $elements, !6 = $results, !7 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cp%3Erandom%3C%2Fp%3E%0A%3Ca+href%3D%22%22%3ETest+1%3C%2Fa%3E+%28target1%29%0A%3Cbr%3E%0A%3Ca+href%3D%22%22%3ETest+2%3C%2Fa%3E++%28target2%29%0A%3Cbr%3E%0A%3Ca+href%3D%22%22%3ETest+3%3C%2Fa%3E+%28skip%29%0A%2F%2F+etc%0A'
   12     1        ASSIGN                                                   !1, <array>
   17     2        INIT_FCALL                                               'implode'
          3        SEND_VAL                                                 '+or+'
          4        INIT_FCALL                                               'array_map'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F7BiUN%3A17%240'
   19     6        SEND_VAL                                                 ~10
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $11     
          9        SEND_VAR                                                 $11
         10        DO_ICALL                                         $12     
   17    11        ASSIGN                                                   !2, $12
   21    12        NEW                                              $14     'DOMDocument'
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !3, $14
   22    15        INIT_METHOD_CALL                                         !3, 'loadHTML'
         16        CONCAT                                           ~17     '%3C%3Fxml+encoding%3D%22utf-8%22+%3F%3E', !0
         17        SEND_VAL_EX                                              ~17
         18        DO_FCALL                                      0          
   23    19        NEW                                              $19     'DOMXPath'
         20        SEND_VAR_EX                                              !3
         21        DO_FCALL                                      0          
         22        ASSIGN                                                   !4, $19
   24    23        INIT_METHOD_CALL                                         !4, 'query'
         24        ROPE_INIT                                     3  ~23     '%2F%2Fa%2Ffollowing-sibling%3A%3Atext%28%29%5B'
         25        ROPE_ADD                                      1  ~23     ~23, !2
         26        ROPE_END                                      2  ~22     ~23, '%5D'
         27        SEND_VAL_EX                                              ~22
         28        DO_FCALL                                      0  $25     
         29        ASSIGN                                                   !5, $25
   25    30        ASSIGN                                                   !6, <array>
   27    31      > FE_RESET_R                                       $28     !5, ->44
         32    > > FE_FETCH_R                                               $28, !7, ->44
   28    33    >   FETCH_OBJ_R                                      ~30     !7, 'previousSibling'
         34        FETCH_OBJ_R                                      ~31     ~30, 'nodeValue'
         35        INIT_ARRAY                                       ~32     ~31
         36        INIT_FCALL                                               'trim'
         37        FETCH_OBJ_R                                      ~33     !7, 'nodeValue'
         38        SEND_VAL                                                 ~33
         39        DO_ICALL                                         $34     
         40        ADD_ARRAY_ELEMENT                                ~32     $34
         41        ASSIGN_DIM                                               !6
         42        OP_DATA                                                  ~32
   27    43      > JMP                                                      ->32
         44    >   FE_FREE                                                  $28
   31    45        INIT_FCALL                                               'print_r'
         46        SEND_VAR                                                 !6
         47        DO_ICALL                                                 
         48      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F7BiUN%3A17%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7BiUN
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        ROPE_INIT                                     3  ~2      'contains%28.%2C+%27'
          2        ROPE_ADD                                      1  ~2      ~2, !0
          3        ROPE_END                                      2  ~1      ~2, '%27%29'
          4      > RETURN                                                   ~1
   19     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F7BiUN%3A17%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.6 ms | 1400 KiB | 21 Q