3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $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; function get_selected($html) { $doc = new DOMDocument; $doc->loadHTML($html); $xpath = new DOMXpath($regex); foreach ($xpath->query("//option") as $option) { $selected_value = $xpath->evaluate('string(@value)', $option); return $selected_value; } } var_dump( get_selected($html1) ); var_dump( get_selected($html2) ); var_dump( get_selected($html3) );
Output for 7.0.0 - 7.2.0
Notice: Undefined variable: regex in /in/betQW on line 17 Fatal error: Uncaught TypeError: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given in /in/betQW:17 Stack trace: #0 /in/betQW(17): DOMXPath->__construct(NULL) #1 /in/betQW(24): get_selected('<option selecte...') #2 {main} thrown in /in/betQW on line 17
Process exited with code 255.
Output for 5.3.21 - 5.6.28
Notice: Undefined variable: regex in /in/betQW on line 17 Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, null given, called in /in/betQW on line 24 and defined in /in/betQW on line 17
Process exited with code 255.