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.

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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样