dongyan6503 2016-11-26 10:21
浏览 61
已采纳

编写多个正则表达式模式来解析HTML [重复]

This question already has an answer here:

I'm fetching an HTML webpage with file_get_contents(), I get a table like below, there are more than 150 rows:

<tr class="tabrow ">
    <td class="tabcol  tdmin_2l">FIRST+DATA</td>
    <td class="tabcol">
        <a class="modal-button" title="SECOND+DATA"  href="THIRD+DATA" rel="{handler: 'iframe', size: {x: 800, y: 640}, overlayOpacity: 0.9, classWindow: 'phocamaps-plugin-window', classOverlay: 'phocamaps-plugin-overlay'}">
            asdxxx
        </a>
    </td>
    <td class="tabcol"></td>
    <td class="tabcol">FOURTH+DATA</td>
</tr>

I want to get the FIRST DATA, SECOND DATA, THIRD DATA and FOURTH DATA with a preg_match_all() call. I tried to write multiple patterns, but I couldn't succeed. Here's what I tried:

preg_match_all('/(<td class="tabcol  tdmin_2l">|title=")(.*?)(<\/td>|")/s', $raw, $matches, PREG_SET_ORDER);

What's the true patterns?

</div>
  • 写回答

2条回答 默认 最新

  • duanpanbo9476 2016-11-26 10:28
    关注

    Try this:

    $str = <<<HTML
    <tr class="tabrow ">
    <td class="tabcol  tdmin_2l">FIRST+DATA</td>
    <td class="tabcol"><a class="modal-button" title="SECOND+DATA"  href="THIRD+DATA" rel="{handler: 'iframe', size: {x: 800, y: 640}, overlayOpacity: 0.9, classWindow: 'phocamaps-plugin-window', classOverlay: 'phocamaps-plugin-overlay'}">asdxxx</a></td>
    <td class="tabcol"></td>
    <td class="tabcol">FOURTH+DATA</td>
    </tr>
    HTML;
    
    preg_match_all('/<td[^>]*>(.*?)<\/td>/im', $str, $td_matches);
    preg_match('/ title="([^"]*)"/i', $td_matches[1][1], $title);
    preg_match('/ href="([^"]*)"/i', $td_matches[1][1], $href);
    
    echo $td_matches[1][0] . "
    ";
    echo $title[1] . "
    ";
    echo $href[1] . "
    ";
    echo $td_matches[1][3];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度