Faisal
Faisal

Reputation: 4765

PHP - Scraping a DIV Element from a Web Page using preg_match

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.

Upvotes: 0

Views: 476

Answers (1)

Sergiu
Sergiu

Reputation: 345

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 [\n\r\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.

Upvotes: 2

Related Questions