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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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.