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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?