Get all <option> s from <select> using getElementById

I need to get all <option> data from <select> in an HTML document using PHP. The code that I have at the moment is:

 $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 to get the value of each parameter from here, as well as the text inside the parameter tags. I cannot validate an object using print_r or similar.

+4
source share
1 answer

You must use the DOM-API to obtain the required data. Since the <select> element is not so complex, you can get 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); 
+4
source

All Articles