dsjfrkvn818747 2014-07-09 12:00
浏览 58
已采纳

preg_match_all用于具有区分大小写代码的表

I was trying to extract railway tickets data for internal use.

Total data looks like this table.

I have extracted every <td> content with preg_match_all condition but I cannot extract coach position as seen in this screenshot

I have tried code like below :

<?php
    $result='tables code over here which you can find in pastebin link';
    preg_match_all('/<TD class="table_border_both"><b>(.*)<\/b><\/TD>/s',$result,$matches);
    var_dump($matches);
?>

I get rubbish output like:

  • 写回答

3条回答 默认 最新

  • dongle7882 2014-07-09 12:13
    关注

    you can use the following regular Expression:

    $re = "/<TD class=\"table_border_both\"><b>([0-9][0-9])
    <\/b><\/TD>/"; 
    $str = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"table_border\">
    
    <tr>
    <td colspan=\"9\" class=\"heading_table_top\">Journey Details</td>
    </tr>
    <TR class=\"heading_table\">
    <td width=\"11%\">Train Number</Td>
    <td width=\"16%\">Train Name</td>
    <td width=\"18%\">Boarding Date <br>(DD-MM-YYYY)</td>
    <td width=\"7%\">From</Td>
    <td width=\"7%\">To</Td>
    <td width=\"14%\">Reserved Upto</Td>
    <td width=\"21%\">Boarding Point</Td>
    <td width=\"6%\">Class</Td>
    </TR>
    <TR>
    <TD class=\"table_border_both\">*12559</TD>
    <TD class=\"table_border_both\">SHIV GANGA EXP </TD>
    <TD class=\"table_border_both\"> 5- 7-2014</TD>
    <TD class=\"table_border_both\">BSB </TD>
    <TD class=\"table_border_both\">NDLS</TD>
    <TD class=\"table_border_both\">NDLS</TD>
    <TD class=\"table_border_both\">BSB </TD>
    <TD class=\"table_border_both\"> SL</TD>
    </TR>
    </table>
    <TABLE width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"table_border\" id=\"center_table\" >
    
    <TR>
    <td width=\"25%\" class=\"heading_table_top\">S. No.</td>
    <td width=\"45%\" class=\"heading_table_top\">Booking Status <br /> (Coach No , Berth No., Quota)</td>
    <td width=\"30%\" class=\"heading_table_top\">* Current Status <br />(Coach No , Berth No.)</td>
    <td width=\"30%\" class=\"heading_table_top\">Coach Position</td>
    </TR>
    <TR>
    <TD class=\"table_border_both\"><B>Passenger 1</B></TD>
    <TD class=\"table_border_both\"><B>S1  , 33,CK    </B></TD>
    <TD class=\"table_border_both\"><B>S1  , 33</B></TD>
    <TD class=\"table_border_both\"><b>11
    </b></TD>
    </TR>
    <TR>
    <TD class=\"table_border_both\"><B>Passenger 2</B></TD>
    <TD class=\"table_border_both\"><B>S1  , 34,CK    </B></TD>
    <TD class=\"table_border_both\"><B>S1  , 34</B></TD>
    <TD class=\"table_border_both\"><b>11
    </b></TD>
    </TR>
    <TR>
    <TD class=\"table_border_both\"><B>Passenger 3</B></TD>
    <TD class=\"table_border_both\"><B>S1  , 36,CK    </B></TD>
    <TD class=\"table_border_both\"><B>S1  , 36</B></TD>
    <TD class=\"table_border_both\"><b>11
    </b></TD>
    </TR>
    <TR>
    <TD class=\"table_border_both\"><B>Passenger 4</B></TD>
    <TD class=\"table_border_both\"><B>S1  , 37,CK    </B></TD>
    <TD class=\"table_border_both\"><B>S1  , 37</B></TD>
    <TD class=\"table_border_both\"><b>11
    </b></TD>
    </TR>
    <TR>
    <td class=\"heading_table_top\">Charting Status</td>
    <TD colspan=\"3\" align=\"middle\" valign=\"middle\" class=\"table_border_both\">   CHART PREPARED   </TD>
    </TR>
    <TR>
    <td colspan=\"4\"><font color=\"#1219e8\" size=\"1\"><b> * Please Note that in case the Final Charts have not been prepared, the Current Status might upgrade/downgrade at a later stage.</font></b></Td>
    </TR>
    </table>"; 
    
    preg_match_all($re, $str, $matches);
    

    Most useful website for regex: http://regex101.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?