用PHP读取文本文件TXT里面信息,然后再拆分出来。
一、文本文件里面的信息是:
Activity Report for customer 0000000085960
Date and Time of Report: 08/12/10 11:01:19
Date and Times Covered by this Report: from  08/11/10 11:00:00 to 08/12/10 11:00:00
Acceptance Date,Time,Destination,PageCount,BaseTid,Attempt,Delivery Date,Time,Status,AccountCode,UserCode
Activity Report for user [email protected] - 0012216038 
Successful Calls
08/12,2:29a,16104547749,1,0121000102236656709,1,08/12,2:30a,,,0122500102236656600
08/12,2:30a,16104547749,1,0121000102236662513,2,08/12,2:33a,,,0122500102236662300
08/12,2:41a,17344169948,1,0951000102236726400,1,08/12,2:42a,,,0952500102236726304
08/12,2:41a,12109461177,1,0911000102236731009,1,08/12,2:42a,,,0912500102236731000
Undeliverable to Destination 
08/12,2:28a,15177830946,1,0121000102236651708,3,08/12,2:38a,0145,,0122500102236651402
There was no activity originating from [email protected] - 0012210830 during this period.
There was no activity originating from [email protected] - 0012230901 during this period.
There was no activity originating from [email protected] - 0012210939 during this period.二、要将文本文件里的
Activity Report for user [email protected] - 0012216038 
Successful Calls
08/12,2:29a,16104547749,1,0121000102236656709,1,08/12,2:30a,,,0122500102236656600
08/12,2:30a,16104547749,1,0121000102236662513,2,08/12,2:33a,,,0122500102236662300
08/12,2:41a,17344169948,1,0951000102236726400,1,08/12,2:42a,,,0952500102236726304
08/12,2:41a,12109461177,1,0911000102236731009,1,08/12,2:42a,,,0912500102236731000
读取拆分成
08/12,2:29a,16104547749,1,0121000102236656709,1,08/12,2:30a,,,0122500102236656600,[email protected]
08/12,2:30a,16104547749,1,0121000102236662513,2,08/12,2:33a,,,0122500102236662300,[email protected]
08/12,2:41a,17344169948,1,0951000102236726400,1,08/12,2:42a,,,0952500102236726304,[email protected]
08/12,2:41a,12109461177,1,0911000102236731009,1,08/12,2:42a,,,0912500102236731000,[email protected]其他信息就不需要了,最好是能写入到另一个TXT文件里面。这个是一个报表的信息,不知哪位大虾能解决小这个问题。小弟全部分点送给你,有不清楚的,写邮件告诉我[email protected]谢谢了。

解决方案 »

  1.   

    $filename = '数据文件';
    $fp = fopen($filename, 'r');
    $outfile = '输出文件';
    file_put_contents($outfile, '');
    $email = '';
    $k = 0;
    while($buf = fgets($fp)) {
      if(preg_match('/Activity .* ([^ ]+@[^ ]+)/', $buf, $reg)) {
        $email = $reg[1];
        $k = 1;
        continue;
      }
      if(preg_match('/^Successful Calls/', $buf)) {
        $k++;
        continue;
      }
      if($k>1 && preg_match('/^\d/', $buf)) {
        file_put_contents($outfile, trim($buf) . ",$email". PHP_EOL, FILE_APPEND);
      }else $k--;}
    fclose($fp);
    readfile($outfile);//检查一下