Php 如何从文本文件中获取每一行并将其放入网站表中?
我有一个文本文件,其中包含可变数量的行,每行包含3个内容,一个IP、浏览器信息和一个日期。基本上,它是一个包含这些内容的访问者日志 我想画一行,得到每个单独的部分,并替换html文档表中的行。到目前为止,我只能从文件中获取第一行 当我试着在while循环中打印一些东西,通过每一行来查看它是否真的通过了多次,它没有。它只回响一次,跑一圈,读一行,然后停下来 文本文件包含以下内容,例如:Php 如何从文本文件中获取每一行并将其放入网站表中?,php,html,str-replace,explode,Php,Html,Str Replace,Explode,我有一个文本文件,其中包含可变数量的行,每行包含3个内容,一个IP、浏览器信息和一个日期。基本上,它是一个包含这些内容的访问者日志 我想画一行,得到每个单独的部分,并替换html文档表中的行。到目前为止,我只能从文件中获取第一行 当我试着在while循环中打印一些东西,通过每一行来查看它是否真的通过了多次,它没有。它只回响一次,跑一圈,读一行,然后停下来 文本文件包含以下内容,例如: 2019/02/12 02:32:56——Mozilla/5.0(Windows NT 10.0;Win64;
- 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
<?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;
?>
在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>
敢
浏览器
知识产权
---日期---
---浏览者---
---知识产权---
我想你也许应该能够像这样阅读文本文件。使用file
打开文本文件将内容放入一个数组中,该数组很容易循环,使用list
结合explode
可以轻松生成变量。我无法确定上面的代码实际上是什么,因为似乎没有输出,也无法理解log.html
安装在何处,但希望这能有所帮助。(未测试)
$textfile='./visitors.txt';
$lines=file($textfile,file_SKIP_EMPTY_line | file_IGNORE_NEW_line);
foreach($line作为$line){
列表($date,$useragent,$ip)=分解('--',$line);
echo$date、$useragent、$ip、“
”;
}
我刚刚解决了它
问题是
$html = file_get_contents("log.html");
及
这两行都必须在while循环中。问题在于如何将数据添加到
$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];
$html=文件获取内容(“log.html”);
//将html拆分为标题、数据行和底部
$html_pieces=explode(“,$html,3);
//显示表格标题
echo$html_片段[0];
$path='./visitors.txt';
$visitor_log=fopen($path,'r');
while(($line=fgets($visitor\u log))!==false){
$string_split=explode(“-”,$line,3);
//从$html\u片段的数据行生成一行新的$output
$output=str_replace(“--date----”,$string_split[0],$html_片断[1]);
$output=str_replace(“--browser----”,$string_split[1],$output);
$output=str_replace(“--ip----”,$string_split[2],$output);
//并逐行显示
echo$输出;
}
//好了,把你的桌子吃完
echo$html_片段[2];
您解决的问题可能与大数据重复。如果log.html
没有改变,那么就没有必要为每一行数据重新读取它,你只是徒劳地敲打你的磁盘。请参阅我的答案以了解避免这种情况的方法。那么您的log.html
包含一组标题和一个空白模板行?你能把它加到你的问题里吗?啊,你刚刚做了:-)让我看看。我已经更新了我的答案。您的$html\u片段已经完成了一半。我的代码的第一个版本中有一个输入错误,并且编辑了几次-您使用的是最新版本吗?如果是,您看到的生成源是什么样子的?这应该能给我们一个错误的线索。:-)很高兴它起了作用。
echo $html;
$html = str_replace("---date---", $string_split[0], $html);
$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];