【发布时间】: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