Jump to content

[SOLVED] Line breaks at a certain letter/number combinations


mcatalf0221

Recommended Posts

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

 

 

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.

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

 

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?

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

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.