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条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算