如何在 PHP 中创建机器可读的日志文件(可能使用 fscanf)

发布于 2024-12-08 11:22:33 字数 1091 浏览 0 评论 0原文

我想用 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;}

此代码会产生三个问题:

  1. 它跳过日志文件中的第一行
  2. 文本只能包含字母和数字(以及我的所有其他特殊字符)已定义),但如果我只能排除分隔符,那么这是一个更好的设计
  3. , 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:

  1. It skips the first line in the logfile
  2. 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
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚风撩人 2024-12-15 11:22:33

从您的示例数据来看,在我看来,最好的方法是内置 csv 函数, fgetcsvfputcsv。只需使用空格而不是逗号作为分隔符即可。这些函数将自动记住引用的值。

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.

绝不放开 2024-12-15 11:22:33

可能有一种更简单的方法可以做到这一点,但是一个不需要分隔符的稍微长的正则表达式解决方案可能是

$reg = /^(\d{10})\s(\d+[\.]\d*)\s"(.*?)"\s\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b\s(\w+)$/;

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

$reg = /^(\d{10})\s(\d+[\.]\d*)\s"(.*?)"\s\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b\s(\w+)$/;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文