dongshenling6585 2009-12-02 23:46
浏览 19

too long

I've trying to preg_match_all a date with slashes in it sitting between 2 html tags; however its returning null.

here is the html:

> <td width='40%' align='right'class='SmallDimmedText'>Last Login: 11/14/2009</td>

Here is my preg_match_all() code

preg_match_all('/<td width=\'40%\' align=\'right\' class=\'SmallDimmedText\'>Last([a-zA-Z0-9\s\.\-\',]*)<\/td>/', $h, $table_content, PREG_PATTERN_ORDER);

where $h is the html above.

what am i doing wrong?
thanks in advance

  • 写回答

4条回答 默认 最新

  • du1108 2009-12-02 23:52
    关注

    It (from a quick glance) is because you are trying to match:

    Last Login: 11/14/2009
    

    With this regex:

    Last([a-zA-Z0-9\s\.\-\',]*)
    

    The regex doesn't contain the required characters of : and / which are included in the text string. Changing the required part of the regex to:

    Last([a-zA-Z0-9\s\.\-\',:/]*)
    

    Gives a match

    Would it be better to simply use a DOM parser, and then preform the regex on the result of the DOM lookup? It makes for nicer regex...

    EDIT

    The other issue is that your HTML is:

    ...40%' align='right'class='SmallDimmedText'>...

    Where there is no space between align='right' and class='SmallDimmedText'

    However your regex for that section is:

    ...40%\' align=\'right\' class=\'SmallDimmedText\'>...

    Where it is indicated there is a space.

    Use a DOM Parser It will save you more headaches caused by subtle bugs than you can count.

    Just to give you an idea on how simple it is to parse using Simple HTML DOM.

    $html = str_get_html(...);
    $elems = $html->find('.SmallDimmedText');
    if ( count($elems->children()) != 1 ){
        throw new Exception('Too many/few elements found');
    }
    $text = $elems->children(0)->plaintext;
    
    //parsing here is only an example, but you have removed all
    //the html so that any regex used is really simple.
    $date = substr($text, strlen('Last Login: '));
    $unixTime = strtotime($date);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行