【问题标题】:How do I get every line from a text file and put it in a website table?如何从文本文件中获取每一行并将其放入网站表中?
【发布时间】:2019-07-07 03:38:03
【问题描述】:

我有一个文本文件,其中包含可变数量的行,每行包含 3 个内容,一个 IP、浏览器信息和一个日期。基本上它是一个包含这些内容的访问者日志。

我想取一行,获取每个单独的部分并替换 html 文档表中的行。截至目前,我只能从文件中获取第一行。

当我试图在遍历每一行的 while 循环中打印一些内容以查看它是否实际上多次通过它时,它没有。它只回显一次,运行一圈,读取一行,然后停止。

文本文件包含例如:

  • 2019/02/12 02:32:56--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1
  • 2019/02/12 02:32:58--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1
  • 2019/02/12 02:33:02--Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36--::1

日期、浏览器信息和 IP。

<?php
// file path
$path = './visitors.txt';
$html = file_get_contents("log.html");

$visitor_log = fopen($path, 'r');
if(flock($visitor_log, LOCK_SH)){
  // Loop genom alla rader i visitors.txt
  while (($line = fgets($visitor_log)) !== false) {
    $html_pieces = explode("<!--==xxx==-->", $html, 3); 
    $string_split = explode("--", $line, 3); 
    $html = str_replace("---date---", $string_split[0], $html); 
    $html = str_replace("---browser---", $string_split[1], $html); 
    $html = str_replace("---ip---", $string_split[2], $html); 
  }
  fclose($visitor_log);
}else{
die("Error! Couldn't read from file.");
}
echo $html;
?>

我不知道为什么循环没有遍历整个文件。有没有办法只回显每一行,看看它是否真的可以读取所有行?

编辑:我刚试过

echo $html;

在while循环中,它会打印出第一行三遍,所以我相信它会遍历所有三行,但它没有得到新数据。有没有关系:

$html = file_get_contents("log.html");

没有更新?

编辑:表格的html代码

<table cellspacing="0" cellpadding="10" border="1" width="100%">
  <thead>
    <tr>
      <th align="left">Dare</th>
      <th align="left">Browser</th>
      <th align="left">IP</th>
    </tr>
  </thead>
  <tbody>
    <!--==xxx==-->
    <tr>
      <td align="left">---date---</td>
      <td align="left">---browser---</td>
      <td align="left">---ip---</td>
    </tr>
    <!--==xxx==-->
  </tbody>
</table>

【问题讨论】:

标签: php html str-replace explode


【解决方案1】:

问题在于如何将数据添加到$html

$html = str_replace("---date---", $string_split[0], $html); 

这会将$html 字符串中的每个---date--- 实例替换为当前正在处理的行的日期。如果$html 是单行模板,则表示将其转换为第一行的一行数据。那会很好用。

但是对于下一行,替换字符串 ---date--- 等不再出现在 $html 中 - $html 现在包含您的第 1 行数据。所以str_replace() 没有找到任何要替换的东西,实际上也不会做任何事情,除了第一行之外你永远看不到任何东西。

最好的解决方案是将模板和生成的结果分开:

$html = file_get_contents("log.html");

// Split up your html into header, data row, and the bottom
$html_pieces = explode("<!--==xxx==-->", $html, 3); 

// Show the table header
echo $html_pieces[0];

$path = './visitors.txt';
$visitor_log = fopen($path, 'r');

while (($line = fgets($visitor_log)) !== false) {
    $string_split = explode("--", $line, 3); 

    // Generate a new line of $output from the data row of your $html_pieces
    $output = str_replace("---date---", $string_split[0], $html_pieces[1]); 
    $output = str_replace("---browser---", $string_split[1], $output); 
    $output = str_replace("---ip---", $string_split[2], $output); 

    // And display, line-by-line
    echo $output;
}

// All done, finish off your table
echo $html_pieces[2];

【讨论】:

    【解决方案2】:

    我想你应该能够像这样阅读文本文件。使用file 打开文本文件将内容放入一个数组中,然后很容易循环并使用list 结合explode 可以很容易地生成变量。我无法真正确定上面的代码实际上是什么,因为似乎没有输出并且无法理解log.html 的位置,但希望这可能会有所帮助。 (未测试)

    $textfile = './visitors.txt';
    $lines = file( $textfile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );
    
    foreach( $lines as $line ){
        list( $date, $useragent, $ip )=explode( '--', $line );
        echo $date,$useragent,$ip,'<br />';
    }
    

    【讨论】:

      【解决方案3】:

      我刚刚解决了!

      问题是

      $html = file_get_contents("log.html");
      

      echo $html;
      

      这两行都必须在while循环中。

      【讨论】:

      • 太好了,你解决了。如果log.html 没有变化,则无需为每一行数据重新读取它,您只是徒劳地敲打磁盘。请参阅我的答案以了解避免这种情况的方法。
      • 所以您的log.html 包含一组标题和一个空白模板行?您可以将其添加到您的问题中吗?啊,你刚刚做到了 :-) 让我看看。
      • 我已经更新了我的答案。你的$html_pieces 已经中途了。
      • 我在我的代码的第一个版本中有错字,并对其进行了几次编辑 - 您使用的是最新版本吗?如果是,您看到的生成源是什么样的?这应该可以提供有关问题所在的线索。
      • :-) 很高兴它有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2018-09-14
      • 2020-05-09
      • 1970-01-01
      相关资源
      最近更新 更多