目前的情況:解析HTML頁面提取場到像數組:值=> option_text
我加載HTML頁面到VAR與的DomDocument
$dom = new DOMDocument('1.0', 'UTF-8');
@$dom->loadHTML($html);
,我需要解析3個選項字段列表。該HTML看起來像這樣:
<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>
現在我發現這堆代碼,但它不工作..
$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\n";
}
問題仍然是:
如何解析HTML頁面提取一個選項選擇的字段,如下所示:value => option_text
我想你會想'//選擇[@name ...'而不是'*/select' – Phil