24722

Retrieving info from a file

Question:

This code retrieves information from another site:

<?php $location = $ident; get_taf($location); function get_taf($location) { $fileName = "ftp://tgftp.nws.noaa.gov/data/forecasts/taf/stations/$location.TXT"; $taf = ''; $fileData = @file($fileName); if ($fileData != false) { list($i, $date) = each($fileData); $utc = strtotime(trim($date)); $time = date("D, jS M Y Hi",$utc); while (list($i, $line) = each($fileData)) { $taf .= ' ' . trim($line); } $taf = trim(str_replace(' ', ' ', $taf)); } if(!empty($taf)){ echo "Issued: $time Z <br><br> $taf"; } else { echo 'TAF not available'; } } ?>

What its <strong>suppose</strong> to look like:

TAF KLBX 160541Z 1606/1706 15009KT P6SM FEW009 BKN013 OVC030 FM160900 15005KT P6SM SCT010 BKN035 FM161600 16010KT P6SM VCSH SCT012 BKN030 FM161900 18012KT P6SM SCT025 BKN040 FM170000 16005KT P6SM SCT012 BKN022

What it ends up looking like:

TAF KLBX 160541Z 1606/1706 15009KT P6SM FEW009 BKN013 OVC030 FM160900 15005KT P6SM SCT010 BKN035 FM161600 16010KT P6SM VCSH SCT012 BKN030 FM161900 18012KT P6SM SCT025 BKN040 FM170000 16005KT P6SM SCT012 BKN022

How do I maintain the spacing with the rows???

It is putting all the info on 1 line and it looks like a paragraoh instead a list.

Thanks

Answer1:

Your output contains newline bytes, but in webpages those line breaks are usually treated as regular spacing characters and not as an actual line break (the br tag is used for hard breaks instead). PHP has a function to convert line breaks to br tags called <a href="http://php.net/manual/en/function.nl2br.php" rel="nofollow">nl2br</a>, so you could do this:

$taf = nl2br(trim(str_replace(' ', ' ', $taf)), false);

Since you're trimming the line endings of every line you'll also have to modify something to either preserve them (by using trim with two parameters or by using just ltrim) or re-add them manually like this:

$taf .= ' ' . trim($line) . "\n";

You could also append the <br> tags directly, that would save you the conversion. Another possibility would be to just preserve/add the line endings and wrap the output in a <pre> section, this will eliminate the need of break-tags.

Answer2:

Modify the following line

$taf .= ' ' . trim($line);

Using the PHP End of Line constant, like so:

$taf .= ' ' . trim($line).PHP_EOL;

Recommend

  • Bitbucket Webhooks
  • What to return when a route/url is found but not the resource behind it?
  • Converting mysql date from one format to another
  • strtotime() converts a non existing date to another date
  • Show a link in fpdf
  • Serializable transactions not protecting me from double inserts
  • SSIS - SQL Server datetimeoffset(0) destination column recognized as DT_WSTR
  • How to access list of email accounts with cPanel API?
  • str_replace with line break? [closed]
  • date.js Parse method overrides Javascript Parse method
  • How to parse utc date
  • String encoding problem on PdoStatement->bindParam()?
  • toInstant() in Calendar is showing in GMT instead of Local time
  • C# “cannot assign field because it is a foreach iteration variable”
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • Excel Date field value differs from c# dateTime by 1 day while reading excel file with EEPlus
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Excel 2007: Format of email address from Outlook 2007
  • Encode string to match encoded form field name in PHP POST array
  • Updating and removing unique join relationships in CakePHP
  • Negating Regex PO BOX
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Jackson Parser: ignore deserializing for type mismatch
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • To Get the radio button value in ruby on rails