0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.