DavChana
DavChana

Reputation: 1976

How to fetch one specific line efficiently from a whole cURL response in php?

I am getting a response $str by using cURL. $str is in html, comprising of always total 327 lines. I want to fetch date from exactly 127th line, which originally is

<b>Last Updated On:</b>17-Dec-2011 11:33:41 UTC<br><br>

To try to reduce the memory used by $str, immidiatly after cURl, i do

$str = strip_tags($str);

Then, to fetch the complete line,

preg_match("/^Last Updated On........................./m", $str, $line);

$lastupdate = $line[0];

Then, to remove the extra text,

$lastupdate = str_replace("Last Updated On:", "", $lastupdate);

Now I think that this preg_match statement is not the most efficient one. It is using too many dots and there must be something like

LookFor = Last Updated On:

If you see LookFor, get next 25 characters.

Am I using correct preg_match syntex? At the moment it is working, and giving me desired result, but I dont know how it is working.

Please suggest me the correct/efficient equivalent of this preg match line.

Also, how can I tell PHP to jump to directly 127th line, instead of going through all the first 126 lines. All lines having [CR][LF] endings.

P.S.

Upvotes: 0

Views: 1642

Answers (1)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Try this :

<?php

    // get the lines in an array
    $lines = explode("\n",$str);

    // get the 127th line
    $line = $lines[126];

    // get the date
    preg_match('/<\/b>(.+)<br><br>/', $line, $matches);

    $date = $matches[1];

    echo $date;

?>

Output :

17-Dec-2011 11:33:41 UTC

Upvotes: 3

Related Questions