我目前尝试使用preg_match只是为了检索 1 个值(在我继续检索多个值之前(,但是,我没有运气。当我执行 print_r(( 时,我的数组中没有存储任何内容。
这是我到目前为止正在尝试的代码:
<?php
$content = '<div class="text-right font-90 text-default text-light last-updated vertical-offset-20">
Reported ETA Received:
<time datetime="2017-02-02 18:12">2017-02-02 18:12</time>
UTC
</div>';
preg_match('|Reported ETA Received: <time datetime=".+">(.*)</time>(.*)(<span title=".+">(.*)<time datetime=".+">(.*)</time></span>)|', $content, $reported_eta_received);
if ($reported_eta_received) {
$arr_parsed['reported_eta_received'] = $reported_eta_received[1];
}
?>
所需输出:
2017-02-02 18:12
我的上述代码不起作用。在这方面的任何帮助将不胜感激。提前谢谢。
不匹配,因为在"报告的 ETA 已接收:"和"<time>
"标记之间有新行。你刚刚在那里放了一个空格(使用 [\r\s\t]+ 代替 " "(。
另外,你为什么不简单地使用:
preg_match('|<time datetime=".*?">(.*?)</time>|', $content, $reported_eta_received);
您还可以使用:?P<name>
以便更轻松地指向(关联与数字:如果放置更多捕获组,数字可能会更改(。
preg_match('|<time datetime=".*?">(?P<name>.*?)</time>|', $content, $match);
print_r($match); // $match['name'] should be there if matched.