scanf formatting issue
I have a log file with the following format:
INFO 2011-03-09 10:26:15,270 [user] message
I want to parse the log file using PHP:
// assume file exists and all that
$handle = fopen("log_file.txt", "r");
while ($line_data = fscanf($handle, "%s %s %s [%s] %s\n")) {
var_dump($line_data);
}
fclose($handle);
When I run this code I get:
[0]=>
array(5) {
[0]=> string(4) "INFO"
[1]=> string(10) "2011-03-09"
[2]=> string(12) "10:26:15,270"
[3]=> string(5) "user]"
[4]=> NULL
}
// snip
It appears the closing bracket in the format string ("%s %s %s [%s] %s") is disrupting the rest of the line from getting parsed. I checked the PHP docs for scanf (as suggested by fscanf), and I didn'开发者_如何转开发t see anything mentioning having to escape a left bracket.
Any suggestions on how to get the 4th and 5th elements to look like "user" and "message" respectively?
Use the format
"%s %s %s [%[^]]] %s\n"
to prevent the 4th element taking any ]
character (of course this assumes there is no user having a ]
in the name).
(Example using sscanf: http://ideone.com/lJHYa)
The %[abc]
format specifiers will make the function read a string consists of only characters a
, b
or c
. The inverse, %[^xyz]
will make the function read a string not having any of x
, y
and z
.
Therefore, the %[^]]
above will read a string until hitting a ]
.
use regular expressions... if you really want fscanf, escape the bracket
http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html :
The bytes between the square brackets (the scanlist) comprise the scanset
精彩评论