在ubuntu 10.04服务器上,我有一个来自php的30mb日志文件。
不幸的是,日志看起来像这样:
...)
Array
(
[calid] => 3114
[email] => somemail@hotmail.com
[firstname] => John
[lastname] => Smith
[address] => Lorem ipsum.
[city] => Lorem
[postcode] => 1345
[date] => 01-12-2013
)
Array
(
[calid] => 3111
[firstname] => Lisa
[lastname] => Smith
[address] => Lorem ipsum.
[city] => Lorem
[postcode] => 4110
[email] => somemail@gmail.com
[phone] => 12345678
[age] => 24
[gender] => female
[customer] => true
[terms] => true
[newsletter] => true
[date] => 01-12-2013
)
Array
(...
我如何将其解析为更有用的东西?一个csv会很好。
答案 0 :(得分:1)
一个可能有用的新想法:
$log = fopen('yourlogfile.log', 'r');
$data = array();
$key = 0;
while (!feof($log))
{
$line = fgets($log);
if(false !== strpos($line, 'Array'))
{
$key++;
$data[$key] = array();
}
$pattern = '/\[(.*?)\] => (.+)/';
preg_match_all($pattern, $line, $matches);
if(!empty($matches[0]))
{
$data[$key][$matches[1][0]] = $matches[2][0];
}
}
fclose($log);
// var_dump($log);
$csv = fopen('yourtarget.csv', 'w+');
for( $i = 1; $i <= count($data); $i++)
{
fputcsv($csv, $data[$i]);
}
fclose($csv);