【发布时间】:2013-03-03 18:58:09
【问题描述】:
Simple HTML DOM 库用于从网页中提取时间戳。然后使用strtotime 将提取的时间戳转换为 MySQL 时间戳。
问题:当strtotime() 用于有效的时间戳时,NULL 被返回(参见2:)。但是,当第二个示例中没有使用 Simple HTML DOM 时,一切正常。
发生了什么,如何解决??
输出:
1:2013-03-03, 12:06PM
2:
3:1970-01-01 00:00:00
var_dump($time)
string(25) "2013-03-03, 12:06PM"
PHP
include_once(path('app') . 'libraries/simple_html_dom.php');
// Convert to HTML DOM object
$html = new simple_html_dom();
$html_raw = '<p class="postinginfo">Posted: <date>2013-03-03, 12:06PM EST</date></p>';
$html->load($html_raw);
// Extract timestamp
$time = $html->find('.postinginfo', 0);
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];
echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
第二个例子
PHP(工作,没有简单的 HTML DOM)
// Extract posting timestamp
$time = 'Posted: 2013-03-03, 12:06PM EST';
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];
echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
输出(正确)
1:2013-03-03, 12:06PM
2:1362312360
3:2013-03-03 12:06:00
var_dump($time)
string(19) "2013-03-03, 12:06PM"
【问题讨论】:
-
能否添加simple_html_dom.php代码?
-
@MIIB 当然:pastebin.com/DxhmsRC1
-
几乎没有相关的可能性,您是否 100%确定您的输入相同?
-
@WesleyMurch 是的,我将时间戳
2013-03-03, 12:06PM EST从第一个示例复制到第二个示例 -
使用 var_dump 而不是 echo 来检查隐藏字符
标签: php web-scraping screen-scraping