如何从文本文件中获取每一行并将其放入网站表中?

问题描述 投票:0回答:3

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

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

当我试图在while循环中打印一些东西时,通过每一行来查看它是否实际多次通过它而不是。它只回响一次,它走了一圈并读取一行然后停止。

文本文件包含例如:

  • 2019/02/12 02:32:56 - Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,与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,与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,与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
3个回答
1
投票

问题在于如何向$html添加数据:

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

这将用---date---字符串中的$html的每个实例替换当前正在处理的行的日期。如果$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];

0
投票

我认为你应该能够像这样阅读文本文件。使用file打开文本文件将内容放入一个数组中,然后使用listexplode结合使用,可以轻松生成变量。我无法确定代码实际上是什么,因为似乎没有输出,无法理解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 />';
}

0
投票

我刚刚解决了!

问题是

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

echo $html;

两行都必须在while循环中。

© www.soinside.com 2019 - 2024. All rights reserved.