使用getElementById从
我需要使用PHP从HTML文档中的<select>获取所有<option>数据。 我目前的代码:
$pageData = $this->Http->get($this->config['url']); libxml_use_internal_errors(true); $this->Dom->loadHTML($pageData); $select = $this->Dom->getElementById('DDteam');我不确定从这里获取每个选项的值以及选项标签内的文本。 我无法使用print_r或类似方法检查对象。
I need to get all of the <option> data from a <select> in an HTML document using PHP. The code that I have at the moment:
$pageData = $this->Http->get($this->config['url']); libxml_use_internal_errors(true); $this->Dom->loadHTML($pageData); $select = $this->Dom->getElementById('DDteam');I am not sure how, from here, to get the value of each of the options and also the text inside the option tags. I can't inspect the object using print_r or similar either.
最满意答案
您必须使用DOM-API来检索所需的数据。 由于<select>元素不是那么复杂,您可以使用getElementsByTagName获取所有<options> -nodes:
$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);You have to use the DOM-API to retrieve the data you want. Since the <select> element is not that complex you can fetch all the <options>-nodes using getElementsByTagName:
$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);更多推荐
发布评论