Faisal Faisal - 4 years ago 103
HTML Question

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:

$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>
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download