mcatalf0221 Posted February 2, 2008 Share Posted February 2, 2008 I am using PHP to import a long string of data. <?php $station = 'KPVD'; $metar = get_metar($station); function get_metar($station) { $fileName = "http://weather.noaa.gov/pub/data/forecasts/taf/stations/KPVD.TXT"; $metar = ''; $fileData = @file($fileName); // or die('Data not available'); if ($fileData != false) { list($i, $date) = each($fileData); $utc = strtotime(trim($date)); while (list($i, $line) = each($fileData)) { $metar .= ' ' . trim($line); } $metar = trim(str_replace(' ', ' ', $metar)); } return $metar; } ?> <html> <body> <?php echo $metar, nl2br(FM); ?> </body> </html> The output of this PHP is as follows: TAF AMD KPVD 021311Z 021312 25008KT P6SM SCT040 FM1400 26015G27KT P6SM SCT040 TEMPO 1417 BKN040 FM2200 26008KT P6SM SKCFM I need to insert a line break before any "FM####" or "TEMPO" occurrence. What code can be used to accomplish this? Thank you! mcatalf0221 Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/ Share on other sites More sharing options...
budimir Posted February 2, 2008 Share Posted February 2, 2008 You can use \n Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456065 Share on other sites More sharing options...
mcatalf0221 Posted February 2, 2008 Author Share Posted February 2, 2008 Actually, looking at the URL where the original data comes from, it appears there are already line breaks (see http://weather.noaa.gov/pub/data/forecasts/taf/stations/KPVD.TXT). Is there something in my code that is not recognizing this? Thanks for any help, I'm very new to PHP for this type of application. Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456067 Share on other sites More sharing options...
kenrbnsn Posted February 2, 2008 Share Posted February 2, 2008 You probably want to use regular expressions. Or you could do something like: <?php $str = 'TAF AMD KPVD 021311Z 021312 25008KT P6SM SCT040 FM1400 26015G27KT P6SM SCT040 TEMPO 1417 BKN040 FM2200 26008KT P6SM SKCFM' echo str_replace(array(' FM',' TEMPO'),array('<br>FM','<br>TEMPO',$str); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456068 Share on other sites More sharing options...
alecks Posted February 2, 2008 Share Posted February 2, 2008 depending on the operating system line breaks are either saved as \n (unix) or \r\n (i think, for windows) EDIT: yeah, http://us3.php.net/manual/en/function.fopen.php Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456074 Share on other sites More sharing options...
mcatalf0221 Posted February 2, 2008 Author Share Posted February 2, 2008 Thanks Ken, but no luck on that one. I tried the following: $str = $metar echo str_replace(array(' FM',' TEMPO'),array('<br>FM','<br>TEMPO',$str); Not sure if that's correct, but the metar is a variable itself because it is taken from that NOAA site which is updated frequently. So, I tried equating the $metar with your $str. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456076 Share on other sites More sharing options...
kenrbnsn Posted February 2, 2008 Share Posted February 2, 2008 Please use tags to surround your code, not tags. I took you script and replaced <?php echo $metar, nl2br(FM); ?> with <?php echo str_replace(array(' FM',' TEMPO'),array('<br>FM','<br>TEMPO'),$metar) ?> and it worked fine. Ken Quote Link to comment https://forums.phpfreaks.com/topic/89051-solved-line-breaks-at-a-certain-letternumber-combinations/#findComment-456084 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.