3v4l.org

run code in 300+ PHP versions simultaneously
<?php // http://stackoverflow.com/questions/34416490/how-to-match-html-selected-option-with-preg-match $html1 = <<<EOT <option selected="selected" value="1">value text</option> EOT; $html2 = <<<EOT <option selected value="1">value text</option> EOT; $html3 = <<<EOT <option value="the devil with **selected** ">value text</option> EOT; $html4 = <<<EOT <option selected="selected" value="1">value text</option> <option value="the devil with **selected** ">value text</option> <option selected value="1">value text</option> EOT; function get_selected($html) { $results = array(); $doc = new DOMDocument; $doc->loadHTML($html); $xpath = new DOMXpath($doc); foreach ($xpath->query("//option") as $option) { $selected_value = $xpath->evaluate('string(@selected)', $option); $results[] = $selected_value; } return $results; } var_dump( '1', get_selected($html1) ); var_dump( '2', get_selected($html2) ); var_dump( '3', get_selected($html3) ); var_dump( '4', get_selected($html4) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fllP7
function name:  (null)
number of ops:  33
compiled vars:  !0 = $html1, !1 = $html2, !2 = $html3, !3 = $html4
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, '%3Coption+selected%3D%22selected%22+value%3D%221%22%3Evalue+text%3C%2Foption%3E'
    8     1        ASSIGN                                                   !1, '%3Coption+selected+value%3D%221%22%3Evalue+text%3C%2Foption%3E'
   11     2        ASSIGN                                                   !2, '%3Coption+value%3D%22the+devil+with+%2A%2Aselected%2A%2A+%22%3Evalue+text%3C%2Foption%3E'
   14     3        ASSIGN                                                   !3, '%3Coption+selected%3D%22selected%22+value%3D%221%22%3Evalue+text%3C%2Foption%3E%0A%3Coption+value%3D%22the+devil+with+%2A%2Aselected%2A%2A+%22%3Evalue+text%3C%2Foption%3E%0A%3Coption+selected+value%3D%221%22%3Evalue+text%3C%2Foption%3E'
   33     4        INIT_FCALL                                               'var_dump'
          5        SEND_VAL                                                 '1'
          6        INIT_FCALL                                               'get_selected'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $8      
          9        SEND_VAR                                                 $8
         10        DO_ICALL                                                 
   34    11        INIT_FCALL                                               'var_dump'
         12        SEND_VAL                                                 '2'
         13        INIT_FCALL                                               'get_selected'
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0  $10     
         16        SEND_VAR                                                 $10
         17        DO_ICALL                                                 
   35    18        INIT_FCALL                                               'var_dump'
         19        SEND_VAL                                                 '3'
         20        INIT_FCALL                                               'get_selected'
         21        SEND_VAR                                                 !2
         22        DO_FCALL                                      0  $12     
         23        SEND_VAR                                                 $12
         24        DO_ICALL                                                 
   36    25        INIT_FCALL                                               'var_dump'
         26        SEND_VAL                                                 '4'
         27        INIT_FCALL                                               'get_selected'
         28        SEND_VAR                                                 !3
         29        DO_FCALL                                      0  $14     
         30        SEND_VAR                                                 $14
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function get_selected:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 25
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/fllP7
function name:  get_selected
number of ops:  28
compiled vars:  !0 = $html, !1 = $results, !2 = $doc, !3 = $xpath, !4 = $option, !5 = $selected_value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        ASSIGN                                                   !1, <array>
   23     2        NEW                                              $7      'DOMDocument'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $7
   24     5        INIT_METHOD_CALL                                         !2, 'loadHTML'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   25     8        NEW                                              $11     'DOMXpath'
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !3, $11
   26    12        INIT_METHOD_CALL                                         !3, 'query'
         13        SEND_VAL_EX                                              '%2F%2Foption'
         14        DO_FCALL                                      0  $14     
         15      > FE_RESET_R                                       $15     $14, ->25
         16    > > FE_FETCH_R                                               $15, !4, ->25
   27    17    >   INIT_METHOD_CALL                                         !3, 'evaluate'
         18        SEND_VAL_EX                                              'string%28%40selected%29'
         19        SEND_VAR_EX                                              !4
         20        DO_FCALL                                      0  $16     
         21        ASSIGN                                                   !5, $16
   28    22        ASSIGN_DIM                                               !1
         23        OP_DATA                                                  !5
   26    24      > JMP                                                      ->16
         25    >   FE_FREE                                                  $15
   30    26      > RETURN                                                   !1
   31    27*     > RETURN                                                   null

End of function get_selected

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162 ms | 1394 KiB | 19 Q