dongyin2643 2017-09-30 18:20
浏览 111
已采纳

PHP preg_match在<td>中提取日期/时间并转换为Unix时间

In my code I am trying to extract date/time only from <td> and then want to convert it into Unix time-stamp but I am stuck at extracting preg_match_all() function it won't show anything when I use $result[0] or $result[1]. I don't know why but when I dump the array I got following result:

My Array

Array
(
    [0] => Array
        (
            [0] => <td align="left" valign="top">02 April 2017 | 05:55 PM</td>
        )

    [1] => Array
        (
            [0] => 02 April 2017 | 05:55 PM
        )

)

Here is my PHP code

preg_match_all('/<td align="left" valign="top">([^"]*)<\/td>/', $printtable, $result);
var_dump($result);
  • 写回答

1条回答 默认 最新

  • dongxin9759 2017-09-30 18:32
    关注

    preg_match_all returns a multidimensional array, not a single element array like preg_match. Your capture group is $result[1][0] (the 1 index is the first capture group, all other indices are additional capture groups). To convert the string to unix time you can use strtotime with str_replace (to get it to a format strtotime can handle, you also could use preg_replace and strip all non alphanumerical/space characters).

    echo strtotime(str_replace('|', '', '02 April 2017 | 05:55 PM'));
    

    Demo: https://3v4l.org/MsOlv

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料