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

Link to comment
Share on other sites

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.";
}

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.