dongqixuan3112 2016-10-19 06:04
浏览 15

php中的preg_match用于选择标签

I want to get all options value between below select tags using preg_match

<select id="current-statement" name="current-statement" data-reactid=".4.2.2.1.0.2.1.1.0">
<option value="current" data-reactid=".4.2.2.1.0.2.1.1.0.0">Current Statement</option>
<option value="90989266-c853-289b-dfea-3cdfe2213db7" data-reactid=".4.2.2.1.0.2.1.1.0.1">1st Statement</option>
<option value="165eb5ea-fd48-53c8-020b-6e3287859922" data-reactid=".4.2.2.1.0.2.1.1.0.2">second statement</option>
<option value="0d558fa0-8f48-afa2-7a9a-e8f85fbbbc42" data-reactid=".4.2.2.1.0.2.1.1.0.3">third statement</option>
<option value="9c78f8aa-3b09-4574-1c10-8f450b45eb5b" data-reactid=".2.0.0.1.0.2.1.1.0.4">4th statement</option>
</select>

I am trying to get it like following preg_match but unable to do so

preg_match("'<select id=\"current-statement\" name=\"current-statement\" data-reactid=\".4.2.2.1.0.2.1.1.0\">(.*?)</select>'", $content, $match);

if($match) echo "result=".$match[1];

Please help

  • 写回答

1条回答 默认 最新

  • dongmo1708 2016-10-19 06:34
    关注

    With DOM, the task is reduced to writing a working XPath expression:

    //select/*/text()
    

    where

    • //select - find all select tags
      • /* - then any child inside
        • /text() - and fetch the text nodes.

    See PHP demo:

    $html = <<<DATA
    <select id="current-statement" name="current-statement" data-reactid=".4.2.2.1.0.2.1.1.0">
    <option value="current" data-reactid=".4.2.2.1.0.2.1.1.0.0">Current Statement</option>
    <option value="90989266-c853-289b-dfea-3cdfe2213db7" data-reactid=".4.2.2.1.0.2.1.1.0.1">1st Statement</option>
    <option value="165eb5ea-fd48-53c8-020b-6e3287859922" data-reactid=".4.2.2.1.0.2.1.1.0.2">second statement</option>
    <option value="0d558fa0-8f48-afa2-7a9a-e8f85fbbbc42" data-reactid=".4.2.2.1.0.2.1.1.0.3">third statement</option>
    <option value="9c78f8aa-3b09-4574-1c10-8f450b45eb5b" data-reactid=".2.0.0.1.0.2.1.1.0.4">4th statement</option>
    </select>
    DATA;
    
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
    $xpath = new DOMXPath($dom);
    $opts = $xpath->query('//select/*/text()');
    $res = array();
    foreach($opts as $opt) { 
       array_push($res, $opt->nodeValue);
    }
    print_r($res);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看