使用getElementById </select> </option>从<select>获取所有<option>

时间:2012-06-08 14:19:48

标签: php html domdocument

我需要使用PHP从HTML文档中的<option>获取所有<select>数据。我现在的代码:

$pageData = $this->Http->get($this->config['url']);
libxml_use_internal_errors(true);
$this->Dom->loadHTML($pageData);
$select = $this->Dom->getElementById('DDteam');

我不确定如何从这里获取每个选项的值以及选项标签内的文本。我无法使用print_r或类似方法检查对象。

1 个答案:

答案 0 :(得分:5)

您必须使用DOM-API来检索所需的数据。由于<select>元素并不复杂,您可以使用getElementsByTagName获取所有<options>个节点:

$select = $this->Dom->getElementById('DDteam');
$options = $select->getElementsByTagName('option');

$optionInfo = array();
foreach($options as $option) {
    $value = $option->getAttribute('value');
    $text = $option->textContent;

    $optionInfo[] = array(
        'value' => $value,
        'text' => $text,
    );
}

var_dump($optionInfo);
相关问题