解析HTML页面以将字段提取到数组中,如:value => option_text

Current scenario :

I'm loading a html page into a var with DomDocument

$dom    = new DOMDocument('1.0', 'UTF-8');  
@$dom->loadHTML($html);

and I need to parse 3 lists of option fields . The HTML looks like this :

<li>
    <select id="advertiser" name="advertiser[]" multiple="multiple" autocomplete="off">                                                                         <option value="35" >Website Adv 1</option>
    <option value="36" >Website Adv 1</option>                                                                                                          <option value="41" >Website Adv 1</option>
    <option value="45" >Website Adv 1</option>
    </select>
</li>

Now I found this code on Stack but it does not work ..

$xpath = new DOMXpath($dom);
$options = $xpath->query("*/select[@name='advertiser[]']/option");
foreach ($options as $option) {
  $optionValue = $option->getAttribute('value');
  $optionContent = $option->nodeValue;
  echo "$optionValue and $optionContent
";
}

The question remains :

How do I parse a HTML page to extract the fields of an option select, into an array like : value=>option_text

The code you posted should work. You can change the code by this

$dom    = new DOMDocument('1.0', 'UTF-8');  
$dom->loadHTML($html);

$xpath = new DOMXpath($dom);
$options = $xpath->query("*/select[@name='advertiser[]']/option");
$result = array();
foreach ($options as $option) {
  $optionValue = $option->getAttribute('value');
  $optionContent = $option->nodeValue;
  $result[$optionValue] = $optionContent;
}

print_r($result);

to load into $result array the items like you want to.

The result should be:

Array
(
    [35] => Website Adv 1
    [36] => Website Adv 1
    [41] => Website Adv 1
    [45] => Website Adv 1
)