2

Code is below. This grabs the webpage into $result just fine. I need to grab just the last line of the data so that I can then manipulate it. What's the best way to do this?

<?php

$curl = curl_init('https://waterdata.usgs.gov/tx/nwis/uv?cb_00060=on&cb_00065=on&format=rdb&site_no=08047500&period=&begin_date=2019-07-18&end_date=2019-07-25');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
echo $result;
1

Normally, I would suggest doing a HEAD request to figure out the Length of the response. Then, download say the last kilobyte or two and parse the last line from there, to save data. However, this server doesn't return the Length when you make a HEAD request. Therefore, you'll just have to download the whole thing.

Fortunately, it isn't very long. You can use RegEx for this job:

preg_match('/\n(.*)$/i', $result, $matches);
echo $matches[1];
3
// get the lines in an array
$lines = explode("\n",$str);

// get the last line
$line = end($lines);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.