我有一个包含以下内容示例的文件,文件有多行,如下所示:
1 月 6 日 10:32:45 id=example sn=0123456789 time="2013-01-06 10:32:46 UTC" fw=1.2.3.4 pri=1 c=0 m=1000 msg="示例行输出" sid =100 cat=TEST-PHP pid=200 src=1.2.3.5:1234:Z1-C444 dst=1.2.3.6:4321:Z1-C444:
我一直在尝试完成的是读取这些行并根据行将它们拆分为数组,然后是基于 var= 值的嵌套数组,例如:
Array (
[0] => Array
(
[0] => "Jan 6 10:32:45"
[id] => "example"
[sn] => "0123456789"
[time] => "2013-01-06 10:32:46 UTC"
[fw] => "1.2.3.4"
[pri] => "1"
[c] => "0"
[m] => "1000"
[msg] => "Example Line Output"
[sid] => "100"
[cat] => "TEST-PHP"
[pid] => "200"
[src] => "1.2.3.5:1234:Z1-C444"
[dst] => "1.2.3.6:4321:Z1-C444:"
)
[1] => Array
(
[1] => "Jan 7 1:50:40"
[id] => "example2"
[sn] => "0123456799"
[time] => "2013-01-07 1:50:41 UTC"
[fw] => "1.2.3.4"
[pri] => "2"
[c] => "2"
[m] => "2000"
[msg] => "Example Line Output 2"
[sid] => "200"
[cat] => "TEST-PHP"
[pid] => "200"
[src] => "1.2.3.7:1234:Z1-C444"
[dst] => "1.2.3.8:4321:Z1-C444:"
)
)
我唯一能做的就是将每一行放入 1 个数组中,我似乎无法让它以上面显示的方式填充。任何帮助将不胜感激。
编辑:这是我的代码,非常简单,读取文件并将每一行拆分为一个数组。
<?php
$filename = "/var/www/html/openfile.log";
// Open the file
$fp = @fopen($filename, 'r');
// Add each line to an array
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
echo "<pre>";
print_r($array);
echo "</pre>";
?>