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 = $purifi开发者_运维技巧er->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.
I believe it does not work because you are forgetting to access the DOMNodeList
's item using its property item
.
Try this approach, loop through the entire length of the DOMNodeList
returned. Checked if the DOMNode
at the current item's index has an attribute named "selected"
$num = 0;
$optionTags = $dom->getElementsByTagName('option');
for ($i = 0; $i < $optionTags->length; $i++ ) {
if ($optionTags->item($i)->hasAttribute('selected')
&& $optionTags->item($i)->getAttribute('selected') === "selected") {
$num = $optionTags->item($i)->nodeValue;
}
}
EDIT:
My exact code:
$dom = new DOMDocument();
$dom->load("C:\\test.htm");
$num = 0;
$optionTags = $dom->getElementsByTagName('option');
for ($i = 0; $i < $optionTags->length; $i++ ) {
if ($optionTags->item($i)->hasAttribute('selected')
&& $optionTags->item($i)->getAttribute('selected') === "selected") {
$num = $optionTags->item($i)->nodeValue;
}
}
echo "Num is " . $num;
Output:
Num is 3
How about using simplexml and XPath selectors?
$xml = new SimpleXMLElement($htmlString);
$result = $xml->xpath('//option[@selected="selected"]');
$option = array_pop($result);
var_dump($option);
(tested, working on PHP 5.3.0)
Your next step in debugging is to verify that $string contains the value you expect. The original code posted is correct.
I was able to get it by doing this:
$xpath->query("//select[@name='foo']/option[@selected]");
This only returned the node which was marked as selected (it didn't have selected="selected")
More elegant code, which goes through all select boxes in html file, finds one which is needed and prints out selected option:
$dom = new DOMDocument();
$dom->loadHTMLFile('somefile.html');
$sel = $dom->getElementsByTagName("select");
foreach ($sel as $select){
if ($select->getAttribute("name") == "someselect") {//find select box with name "someselect"
$optionTags = $select->getElementsByTagName('option');
foreach ($optionTags as $tag){
if ($tag->hasAttribute("selected"))
echo $tag->nodeValue;
}
}
精彩评论