【问题标题】:How to get a line in a txt file and echo it out?如何在 txt 文件中获取一行并将其回显?
【发布时间】:2017-12-30 02:11:24
【问题描述】:

我有一个包含一些温度的 txt 文件:

26 C  
25.06 C  
25.00 C  
25.00 C  
25.00 C  
25.00 C  
24.94 C  
24.94 C  
24.94 C  
24.94 C  
24.94 C  
24.94 C  

我希望这些行在我的 php/html 文件的表格中回显:

<table>
        <tr>
            <th>Date</th>
            <th>Time</th>
            <th>TEMP</th>
        </tr>
        <tr>
            <td>Date</td>
            <td>Time</td>
            <td>TEMP</td>
        </tr>
    </table>

我该怎么做?

【问题讨论】:

标签: php html


【解决方案1】:

您没有提供任何有关如何获取日期和时间的示例数据,但给出了您的 temps.txt

<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Time</th>
            <th>TEMP</th>
        </tr>
    </thead>
    <tbody>

<?php
foreach (file('temps.txt') as $temp) {
    echo "
        <tr>
            <td>Date</td>
            <td>Time</td>
            <td>$temp</td>
        </tr>
    ";
}
?>

    </tbody>
</table>

如果您的文件还包含日期/时间信息,那么您可能有兴趣使用带有 fgetcsv 的 CSV 格式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 2019-06-22
    • 2012-06-17
    • 1970-01-01
    • 2016-08-19
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多