dongling5411 2013-12-02 10:02
浏览 24
已采纳

解析文本文件以创建有用的日志文件

On an ubuntu 10.04 server, i have a 30mb log file from php.

Unfortunately the log looks like this:

...)
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
(...

How can i parse this into something more useful? A csv would be nice.

  • 写回答

1条回答 默认 最新

  • douhuan1648 2013-12-02 11:12
    关注

    A raw idea that may be helpful as a start:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,