简体   繁体   中英

PHP Reading and Parsing Files

I have a file with the sample below contents, file has multiple lines like the below:

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:

What I have been trying to accomplish is read these lines in and split them into arrays based on the line, then a nested array based on the var= value, for example:

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:"
      )
)

The only thing I can get it to do is put each line into 1 array, I can't seem to get it to populate the way it is shown above. Any help would be greatly appreciated.

EDIT: This is the code I have, very simple, read the file and split each line into an array.

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

Took me some time but I've made you REGEX and I've used preg_match function

$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);

Output:

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: 
)

Now you can create array like

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

Subpatterns should suit your needs. Check regex it's easy. mostly 0-9, AZ etc.

You can also put it in loop and add new arrays with

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

Edit

WORKING DEMO

Most of your answer is with parse_str. You need to add a name for the first variable though and you need ampersands instead of spaces. So, this is the simplistic answer:

$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);

The problem here is that it breaks up the values with spaces in them. So, we need to replace the spaces before doing parse_string. This is what I'd do instead of the simple str_replace above:

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);

It first replaces all spaces between quotes with NOT_A_SPACE. Then, it replaces spaces with an ampersand. Then, it replaces NOT_A_SPACE with a space.

something similar to your script

$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]), '"')
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM