Reputation: 3193
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;
Upvotes: 2
Views: 739
Reputation: 3507
// get the lines in an array
$lines = explode("\n",$str);
// get the last line
$line = end($lines);
Upvotes: 3
Reputation: 163438
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];
Upvotes: 1