从文件中检索信息 [英] Retrieving info from a file

查看:96
本文介绍了从文件中检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码从另一个站点检索信息:

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';
    }
}

?>

假设是什么样的:

 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

最终效果如下:

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???

它将所有信息都放在一行上,它看起来像一个paragraoh而不是一个列表.

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

谢谢

推荐答案

您的输出包含换行符字节,但是在网页中,这些换行符通常被视为常规的空格字符,而不是实际的换行符(br标记为用于硬休息). PHP具有将换行符转换为br标记的功能,称为 nl2br ,因此您可以执行以下操作:

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 nl2br, so you could do this:

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

由于要修剪每行的行尾,因此还必须修改某些内容以保留它们(通过使用带有两个参数的trim或仅使用ltrim)或手动重新添加它们这个:

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";

您还可以直接附加<br>标记,这样可以节省转换.另一种可能性是仅保留/添加行尾并将输出包装在<pre>节中,这将消除中断标记的需要.

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.

这篇关于从文件中检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆