简体   繁体   中英

PHP scraping & preg_replace

I'm scraping weather information of one meteo station and trying to convert it into database format.

I scraped from the web page the data I need, but when I try to replace all the matches in my code it only does it once and also I still don't know how to convert it into an array. the array i'm trying to do is something like (date, time, variable, value). I need the array to put it into a .txt file and a database later.

this is what i've got so far:

 <?php;

 function scrape_between($data, $start, $end){
    $data = stristr($data, $start); // miro que quito todo antes de start
    $data = substr($data, strlen($start));  // Stripping $start
    $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
    $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
    return $data;   // devuelvo data entre las sentencias

}
// Defining the basic cURL function
$url = 'http://aprs.fi/?c=raw&call=EA4RKU-13&limit=1000&view=normal';
$ch = curl_init();  // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url);    // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable

$start="<div class='browselist_data'>";
$end = '</div>';

$data = scrape_between($data, $start, $end); // me quedo con la parte con span

curl_close($ch);    // Closing cURL
//echo strip_tags($data); // quita todos los tags html y php deja feo

 $z = count($data);

 echo "numero de datos: " .$z; //echo $data;   // Returning the data from the function
 $data1=preg_replace('/<b>.+?<\/b>/im', '' ,$data);
 //$data1=preg_replace('/<b>.+?<\/b>/im', '' ,$data);
 $data1=preg_replace('/$<a>.+?<\/a>/', '', $data1);
 echo $data1;

 ?>

Instead of the stristr and stripos, you could use something like this:

<?
  ...

  preg_match_all("|<span class='raw_line'>(.*?)</span>|is",$data,$lines);
  echo count($lines[1]); //number of lines

?>

Also, you should explain better what do you need from each line? What kind of output do you expect? Can you show an example?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM