PHP preg_match在中提取日期/时间并转换为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);
在我的代码中,我只想从&lt; td&gt; code>中提取日期/时间 然后想把它转换成Unix时间戳,但是我坚持解析 preg_match_all() code>函数,当我使用 $ result [0] code>时它不会显示任何内容 $结果[1] 代码>。 我不知道为什么,但是当我转储数组时,我得到了以下结果: p>
我的数组 p>
数组
(
[0] =&gt;数组
(
[0] =&gt;&lt; td align =“left”valign =“top”&gt; 02 April 2017 | 05:55 PM&lt; / td&gt;
)
[1] =&gt;数组
(
[0] =&gt; 2017年4月2日| 05:55 PM
)
)
code> pre>
这是我的PHP代码 p>
preg_match_all('/&lt; td align =“left”valign =“top”&gt;([^“] *)&lt; \ / td&gt; /',$ printtable,$ result);
var_dump($ result);
code> pre>
div>
答
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
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);
在我的代码中,我只想从 我的数组 p>
这是我的PHP代码 p>
&lt; td&gt; code>中提取日期/时间 然后想把它转换成Unix时间戳,但是我坚持解析
preg_match_all() code>函数,当我使用
$ result [0] code>时它不会显示任何内容 $结果[1] 代码>。 我不知道为什么,但是当我转储数组时,我得到了以下结果: p>
数组
(
[0] =&gt;数组
(
[0] =&gt;&lt; td align =“left”valign =“top”&gt; 02 April 2017 | 05:55 PM&lt; / td&gt;
)
[1] =&gt;数组
(
[0] =&gt; 2017年4月2日| 05:55 PM
)
)
code> pre>
preg_match_all('/&lt; td align =“left”valign =“top”&gt;([^“] *)&lt; \ / td&gt; /',$ printtable,$ result);
var_dump($ result);
code> pre>
div>
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