1

我有一个包含以下内容示例的文件,文件有多行,如下所示:

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>";
?>
4

3 回答 3

0

花了我一些时间,但我给你做了 REGEX 并且我使用了preg_match函数

$matches = array();
$str = '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:';
preg_match('/([A-Za-z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+) id=([a-z]+) sn=([0-9]+) time="([^"?]+)" fw=([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) pri=([0-9]+) c=([0-9]+) m=([0-9]+) msg=("[^"]+") sid=([0-9]+) cat=([A-Z\-]+) pid=([0-9]+) src=([0-9]+.[0-9]+.[0-9]+.[0-9]:[0-9]+:[A-Z][0-9]-[A-Z][0-9]+) dst=([0-9]+.[0-9]+.[0-9].[0-9]:[0-9]+:[A-Z][0-9]-[A-Z][0-9]+:)/', $str, $matches);
print_r($matches);

输出:

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] => Jan 6 10:32:45 
 [2] => example 
 [3] => 0123456789 
 [4] => 2013-01-06 10:32:46 UTC 
 [5] => 1.2.3.4 
 [6] => 1 
 [7] => 0 
 [8] => 1000 
 [9] => "Example Line Output" 
 [10] => 100 
 [11] => TEST-PHP 
 [12] => 200 
 [13] => 1.2.3.5:1234:Z1-C444 
 [14] => 1.2.3.6:4321:Z1-C444: 
)

现在您可以创建数组

$array = array( 0 => $matches[1], 'id' => $matches[2], 'sn' => $matches[3] .... );

子模式应该适合您的需求。检查正则表达式很容易。主要是0-9,AZ等。

您也可以将其放入循环并添加新数组

$main_array = array();
for(..)
{
  $main_array[] = $array; //where array is array of regex matches
}

编辑

工作演示

于 2013-06-07T13:18:02.747 回答
0

您的大部分答案都是使用 parse_str。您需要为第一个变量添加一个名称,并且您需要与号而不是空格。所以,这是一个简单的答案:

$str = ...whatever your string is...;
$str = "date=".$str; // Add a name for the first date
$str = str_replace(' ','&',$str);
parse_string($str, $array_of_names_and_values);

这里的问题是它用空格分解了值。因此,我们需要在执行 parse_string 之前替换空格。这就是我要做的,而不是上面的简单 str_replace:

for($oldstr = $str; $oldstr != ($str = preg_replace('/(="[^" ]*) /', '\1NOT_A_SPACE', $str)); $oldstr = $str);
$str = preg_replace('/\s+/', '&', $str);
$str = str_replace('NOT_A_SPACE', ' ', $str);

它首先用 NOT_A_SPACE 替换引号之间的所有空格。然后,它用 & 号替换空格。然后,它将 NOT_A_SPACE 替换为空格。

于 2013-06-07T13:21:07.937 回答
0

类似于你的脚本的东西

$var = file('openfile.log');

$output = array();
foreach($var as $v){
$datainthisline = trim($v);
if($datainthisline != '(' && $datainthisline != ')' && $datainthisline != 'Array (' ){
    if(strpos($datainthisline, 'Array')){
        $pararrvar = explode("=>", $datainthisline);
        $pararrvar_key = str_replace(array('[',']'), "", $pararrvar[0]);
    }
    else{
        $valarrvar = explode("=>", $datainthisline);
        $valarrvar_key = str_replace(array('[',']'), "", $valarrvar[0]);
        $output[trim($pararrvar_key)][trim($valarrvar_key)] = trim(trim($valarrvar[1]), '"')
    }
}
}
于 2013-06-07T13:30:21.770 回答