Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-DOM-Get<;选项>;选择的标记_Php_Dom - Fatal编程技术网

PHP-DOM-Get<;选项>;选择的标记

PHP-DOM-Get<;选项>;选择的标记,php,dom,Php,Dom,假设HTML看起来像这样: <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> <

假设HTML看起来像这样:

<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>

1.
2.
3.
4.
我需要从那里提取属性为selected=“selected”的选项标记。我该怎么做?到目前为止,我有:

$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;
$string=file\u get\u contents('test.html');
包括“htmlpurifier-4.0.0-standalone/htmlpurifier.standalone.php”;
$config=HTMLPurifier_config::createDefault();
$config->set('HTML.Doctype','XHTML1.0-Strict');
$punizer=新的HTMLPurifier($config);
$string=$purizer->purify($string);
$dom=新的DOMDocument();
$dom->loadHTML(“”.$string);
$dom->preserveWhiteSpace=false;
$num=0;
$optionTags=$dom->getElementsByTagName('option');
foreach($optionTags作为$o){
如果($o->hasAttribute('selected'))
&&'选定'=$o->getAttribute('选定')){
$num=$o->nodeValue;
}
}
echo$num;

这是行不通的。$num之后仍然等于零。

使用simplexmlXPath选择器如何

$xml = new SimpleXMLElement($htmlString);
$result = $xml->xpath('//option[@selected="selected"]');

$option = array_pop($result);
var_dump($option);

(经过测试,在PHP5.3.0上工作)

我认为它不起作用,因为您忘记了使用
DOMNodeList
的属性
item
访问它的项

尝试这种方法,遍历返回的
DOMNodeList
的整个长度。检查当前项索引处的
DOMNode
是否具有名为“selected”的属性

$num=0;
$optionTags=$dom->getElementsByTagName('option');
对于($i=0;$i<$optionTags->length;$i++){
如果($optionTags->item($i)->hasAttribute('selected'))
&&$optionTags->item($i)->getAttribute('selected')=“selected”){
$num=$optionTags->item($i)->nodeValue;
}
}
编辑: 我的确切密码:

$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;
$dom=newdomdocument();
$dom->load(“C:\\test.htm”);
$num=0;
$optionTags=$dom->getElementsByTagName('option');
对于($i=0;$i<$optionTags->length;$i++){
如果($optionTags->item($i)->hasAttribute('selected'))
&&$optionTags->item($i)->getAttribute('selected')=“selected”){
$num=$optionTags->item($i)->nodeValue;
}
}
回显“Num is”$号码;
输出:


Num为3

调试的下一步是验证$string是否包含预期的值。发布的原始代码是正确的。

我可以通过以下操作获得它:

$xpath->query("//select[@name='foo']/option[@selected]");

这只返回被标记为选中的节点(它没有select=“selected”)

更优雅的代码,它遍历html文件中的所有选择框,找到所需的一个并打印出所选选项:

$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;
    }
}

试着用
==
而不是
==
看看这是否有效,如果不行,我会运行一个测试,看看我是否能让它工作。我试过了,但还是没用。顺便说一下,确切的HTML来自这个页面:当我回显$optionTags->length时;它打印0。。。这就是为什么循环没有运行一次的原因。我会放置
var\u dump
来查看
$string
$dom
的内容,有些东西不起作用。我没有使用HTMLPurifier,使用它的原因是什么。HTMLPurifier出于某种原因正在剥离整个内容,尽管它似乎是有效的XHTML。
$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;
    }
}