dongwei8729 2017-02-04 19:24
浏览 70
已采纳

PHP - 使用preg_match从网页上刮取DIV元素

I am trying to use preg_match currently just to retrieve 1 value (before I move onto retrieving multiple values), however, I am having no luck. When I perform a print_r() there is nothing stored in my array.

Here is my code what i am trying so far:

<?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];
}
?>

Required Output:

2017-02-02 18:12

My above-mentioned code is not working. Any help on this regards would be appreciated. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • drwo32555 2017-02-04 20:02
    关注

    It may not match because there is a new line between Reported ETA Received: and the <time> tag. And you've just put in there a space (use [ \s\t]+ instead " ").

    Also, why don't you simply use:

    preg_match('|<time datetime=".*?">(.*?)</time>|', $content, $reported_eta_received);

    You can also use:?P<name> for a easier pointing (associative vs numeric: numeric can change if you put more capture groups).

    preg_match('|<time datetime=".*?">(?P<name>.*?)</time>|', $content, $match); print_r($match); // $match['name'] should be there if matched.

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

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目