如何在 PHP 中创建机器可读的日志文件(可能使用 fscanf)
我想用 PHP 创建一个简单的日志文件,稍后可以通过 PHP 读取。
该日志文件应如下所示:
1303942306 26.4 "Lore ipsum 1" 84.169.73.55 appname1
1303942308 28.8 "Lore ipsum 2" 84.169.73.55 appname2
1304078658 31.2 "Lore ipsum 3" 80.187.103.95 appname3
正如您所看到的,它应该包含以下变量:
$timestamp
$float
$text (with spaces and other special characters)
$ip
$appname
到目前为止,无需在此日志文件中保存文本(带空格),因此我可以使用简单的 fscanf 函数。
我尝试过使用“|”作为 fscanf 的分隔符 - 但是,它有一些“小故障”;-):
//write to file
$statisticsfile = fopen("user.history",'a+b');
fputs($statisticsfile, time().' | '.$float.' | '.$text.' | '.$_SERVER['REMOTE_ADDR'].' | '.$appname."\r\n");
//read from file
while ($userinfo = fscanf($statisticsfile, "%[0-9] | %[0-9.-] | %[a-zA-Z0-9 ,] | %[0-9.] | %[0-9]"))
{list ($timestamp[], $float[], $text[], $ip[], $appname[]) = $userinfo;}
此代码会产生三个问题:
- 它跳过日志文件中的第一行
- 文本只能包含字母和数字(以及我的所有其他特殊字符)已定义),但如果我只能排除分隔符,那么这是一个更好的设计
- , fscanf 真的是此任务的最佳(最快,内存消耗最少)函数吗?或者带有分隔符的简单 fget 函数会更好吗?
我非常感谢您的帮助;-)
I want to create a simple logfile in PHP that later can be read by PHP.
This logfile should look like this:
1303942306 26.4 "Lore ipsum 1" 84.169.73.55 appname1
1303942308 28.8 "Lore ipsum 2" 84.169.73.55 appname2
1304078658 31.2 "Lore ipsum 3" 80.187.103.95 appname3
As you can see it is supposed to contain the following variables:
$timestamp
$float
$text (with spaces and other special characters)
$ip
$appname
Until now there was no need to save text (with spaces) within this logfile, so I could use a simple fscanf function.
I've tried to use "|" as a delimiter with fscanf - but, well, it has a few "glitches" ;-):
//write to file
$statisticsfile = fopen("user.history",'a+b');
fputs($statisticsfile, time().' | '.$float.' | '.$text.' | '.$_SERVER['REMOTE_ADDR'].' | '.$appname."\r\n");
//read from file
while ($userinfo = fscanf($statisticsfile, "%[0-9] | %[0-9.-] | %[a-zA-Z0-9 ,] | %[0-9.] | %[0-9]"))
{list ($timestamp[], $float[], $text[], $ip[], $appname[]) = $userinfo;}
This code creates three problems:
- It skips the first line in the logfile
- the text can only contain letters and number (and all other special characters I have defined), but it such a better design if I could only exclude the delimiter
- is fscanf really the best (fastest, least memory-consuming) function for this task? Or would a simple fget function with a delimiter be better?
I would really appreciate your help ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的示例数据来看,在我看来,最好的方法是内置 csv 函数, fgetcsv 和 fputcsv。只需使用空格而不是逗号作为分隔符即可。这些函数将自动记住引用的值。
From your sample data, seems to me that the best way to go is the built-in csv functions, fgetcsv and fputcsv. Simply use a space as your delimiter instead of a comma. These functions will automatically be mindful of the quoted values.
可能有一种更简单的方法可以做到这一点,但是一个不需要分隔符的稍微长的正则表达式解决方案可能是
ip 捕获组使它看起来比实际情况更糟糕。
然后你可以用它来制作和制作像这样断行文本...
http://ideone.com/hCvPy
There is probably a much easier way to do this, but one slightly long regex solution to not needing a delimiter could be
the ip capturing group makes it look worse then it is.
which you could then use to make & break lines of text like so...
http://ideone.com/hCvPy