Jump to content

Preg_match line and break up parts of a line into variables


michael.davis

Recommended Posts

Happy Wednesday Everyone!  Thank you in advance for helping me with this.

 

I am grabbing a file from the internet, and trying to gather the line data associated with one location from it.  I want to break out the individual parts of that line and put into a variable to use.  Here is my code below:

 

<?php
    $data = file_get_contents('http://www.srh.noaa.gov/productview.php?pil=OHXRWROHX');
    echo $data;
if ( preg_match("/NASHVILLE/", $data, $match) ) {
    echo "A match was found.";
$site = $match[0];
$temp = $match[1];
echo "Site: " . $site;
echo "Temp: " . $temp;
echo "<br />";
} else {
    echo "A match was not found.";
}

?>

 

Thanks again for your help.

Mike

Still digging on this.  There is a line in the file that I am pulling in:

 

NASHVILLE      PTCLDY    62  58  86 CALM      30.13R

 

I am trying to grab only this line... and break it up into segments and put into variables.  I am able to preg match the NASHVILLE word, but not sure how to break up only this line and have each segment as a variable. 

 

Does that make sense?

 

 

 

?php
    $data = file_get_contents('http://www.srh.noaa.gov/productview.php?pil=OHXRWROHX');
    echo $data;

if ( preg_match("/NASHVILLE/", $data, $match) ) {
    echo "A match was found.";
$site = $match[0];
$temp = $match[2];
    echo "Site: " . $site;
    echo "Temp: " . $temp;
} else {
    echo "A match was not found.";
}

?>

 

 

Looking to the explode function.  Thanks!  Just not sure how to grab just that one line.  :confused:

 

Yup, considered that after the post.

 

Once again, can their be more than one occurance of something you'd search for? IE. can "Nashville" appear more than once?

 

Is using a database possible? This is exactly the kind of work databases are meant for.

Understand.  Nashville is used once.

 

The City name stays the same length but the data in the other columns besides the city name are not fixed  .  Using the explode function should work.  Does the explode function only used fixed spaces or will explode adjust for the white spacing differences?

<?php

$data = file_get_contents('http://www.srh.noaa.gov/productview.php?pil=OHXRWROHX');
$city = 'Nashville';

// NASHVILLE      MOSUNNY   74  60  61 CALM      30.15R
// BURNS*           N/A    N/A N/A N/A S1          N/A
$expr = "%$city*?\s+
([a-z/]+)\s+
(n/a|\d+)\s+
(n/a|\d+)\s+
(n/a|\d+)\s+
([a-z0-9]+)\s+
(n/a|[0-9.]+[a-z])%ix";

preg_match( $expr, $data, $match );

print_r($match);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.