michael.davis Posted August 22, 2012 Share Posted August 22, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 22, 2012 Share Posted August 22, 2012 And? Quote Link to comment Share on other sites More sharing options...
michael.davis Posted August 22, 2012 Author Share Posted August 22, 2012 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."; } ?> Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 22, 2012 Share Posted August 22, 2012 Use explode: http://ua2.php.net/manual/en/function.explode.php Can more than one line match your criteria? IE. can there be more than one line that starts with "NASHVILLE"? Quote Link to comment Share on other sites More sharing options...
michael.davis Posted August 22, 2012 Author Share Posted August 22, 2012 Looking to the explode function. Thanks! Just not sure how to grab just that one line. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 22, 2012 Share Posted August 22, 2012 Looking to the explode function. Thanks! Just not sure how to grab just that one line. 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. Quote Link to comment Share on other sites More sharing options...
michael.davis Posted August 22, 2012 Author Share Posted August 22, 2012 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? Quote Link to comment Share on other sites More sharing options...
michael.davis Posted August 22, 2012 Author Share Posted August 22, 2012 This link is better for what I am doing: http://www.srh.noaa.gov/productview.php?pil=OHXRWRMEG Quote Link to comment Share on other sites More sharing options...
xyph Posted August 22, 2012 Share Posted August 22, 2012 <?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); ?> Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted August 22, 2012 Share Posted August 22, 2012 Oh, I should have paid closer attention to the link, glad someone did! Quote Link to comment Share on other sites More sharing options...
michael.davis Posted August 22, 2012 Author Share Posted August 22, 2012 Thank you for the help! I was trying my darnest to display just that one line, then to get explode to work. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.