doupo1865 2013-10-13 18:01
浏览 151

解析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

  • 写回答

1条回答 默认 最新

  • duanmu3049 2013-10-13 18:10
    关注

    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
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用