So let's say the HTML looks something like this:
<select name="some_name">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
</select>
I need to extract the option tag with attribute selected="selected" from there. How can I do that? So far I have this:
$string = file_get_contents('test.html');
include 'htmlpurifier-4.0.0-standalone/HTMLPurifier.standalone.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Doctype', 'XHTML 1.0 Strict');
$purifier = new HTMLPurifier($config);
$string = $purifier->purify($string);
$dom = new DOMDocument();
$dom->loadHTML('<?xml encoding="UTF-8">' . $string);
$dom->preserveWhiteSpace = false;
$num = 0;
$optionTags = $dom->getElementsByTagName('option');
foreach ($optionTags as $o) {
if ($o->hasAttribute('selected')
&& 'selected' === $o->getAttribute('selected')) {
$num = $o->nodeValue;
}
}
echo $num;
And that doesn't work. The $num is still equal to zero afterwards.