解析固定格式的文件数据 php脚本

#!/usr/bin/php
<?php
function printUseage() {
    die("usage: ./txt2json.php  [file_in] [file_out] [id:0,(ALL USER)] [search_type:1(ITEM),2(RES),3(ITEMOVERFLOW)] [res_id:0(ALL id)]\n");
}
function checkSearchCond($origin, $cond) {
    if ($cond == 0) {
        return true;
    }
    if ($origin == $cond) {
        return true;
    }
    return false;
}


if ($argc != 6) {
    printUseage();
}
$in_file = "";
$out_file = "";
$user_id = "";
$search_type = "";
$res_search_id = "";
if ($argv[1] == "") {
    printUseage();
} else {
    $in_file = $argv[1];
}
if ($argv[2] == "") {
    printUseage();
} else {
    $out_file = $argv[2];
}
if ($argv[3] != "") {
    $user_id = $argv[3];
} else {
    printUseage();
}
if ($argv[4] != "") {
    $search_type = $argv[4];
} else {
    printUseage();
}
if ($argv[5] != "") {
    $res_search_id = $argv[5];
} else {
    printUseage();
}
if ($search_type != 1 && $search_type != 2 && $search_type != 3) {
    echo "ERROR: search_type range must be in (1,2,3) !!!\n";
    printUseage();
}
if (!file_exists($in_file)) {
    echo "ERROR: file "."$in_file"." doesn't  exists!!!\n";
    printUseage();
}
$file = file_get_contents($in_file);
//$file = iconv('UCS-2LE', 'UTF-8', $file);
$out_log_file = fopen($out_file, "w") or die("unable to open file!");


$lines = explode("\n", $file);
$time_array = array();
$id_array = array();
$user_name_array = array();
$way_type_array = array();
$way_desc_array = array();
$res_template_array = array();
$change_value_array = array();
$res_value_array = array();
foreach ($lines as $s) {
    if ($line = explode("|", $s)) {
        if ($search_type == 1) {//ITEM
            if ($line[0] == "ITEM" && checkSearchCond($line[6], $res_search_id)) {
                if (checkSearchCond($line[2], $user_id)) {
                    $time_array[] = $line[1];
                    $id_array[] = $line[2];
                    $user_name_array[] = $line[3];
                    $way_type_array[] = $line[4];
                    $way_desc_array[] = $line[5];
                    $res_template_array[] = $line[6];
                    $change_value_array[] = $line[7];
                    $res_value_array[] = $line[8];
                }
            }
        } elseif ($search_type == 2) {//RES
            if ($line[0] == "RES" && checkSearchCond($line[6], $res_search_id)) {
                if (checkSearchCond($line[2], $user_id)) {
                    $time_array[] = $line[1];
                    $id_array[] = $line[2];
                    $user_name_array[] = $line[3];
                    $way_type_array[] = $line[4];
                    $way_desc_array[] = $line[5];
                    $res_template_array[] = $line[6];
                    $change_value_array[] = $line[7];
                    $res_value_array[] = $line[8];
                }
            }
        } elseif ($search_type == 3) {//ITEMOVERFLOW
            if ($line[0] == "ITEMOVERFLOW" && checkSearchCond($line[6], $res_search_id)) {
                if (checkSearchCond($line[2], $user_id)) {
                    $time_array[] = $line[1];
                    $id_array[] = $line[2];
                    $user_name_array[] = $line[3];
                    $way_type_array[] = $line[4];
                    $way_desc_array[] = $line[5];
                    $res_template_array[] = $line[6];
                    $res_value_array[] = $line[7];
                }
            }
        }
    }
}
date_default_timezone_set("Asia/Shanghai");
$total = count($time_array);
$header = "";
if ($search_type == 1) {
    $header = "TIME\tNAME\tWAY\tITEM_ID\tCHANGE\tNOWVALUE\n";
} elseif ($search_type == 2) {
    $header = "TIME\tNAME\tWAY\tRES_ID\tCHANGE\tNOWVALUE\n";
} elseif ($search_type == 3) {
    $header = "TIME\tNAME\tWAY\tITEM_ID\tLOSTVALUE\n";
}
fwrite($out_log_file, $header);
if ($search_type == 1 || $search_type == 2) {
    for ($i=0; $i<$total; ++$i)
    {
        $time_stamp = date('Y-m-d H:i:s', $time_array[$i]);
        $user_name = $user_name_array[$i];
        $way_desc = $way_type_array[$i];
        $res_id = $res_template_array[$i];
        $changed = $change_value_array[$i];
        $new_value = $res_value_array[$i];
        $out = "$time_stamp\t$user_name\t$way_desc\t$res_id\t$changed\t$new_value";
        fwrite($out_log_file, $out."\n");
    }
} elseif ($search_type == 3) {
    for ($i=0; $i<$total; ++$i)
    {
        $time_stamp = date('Y-m-d H:i:s', $time_array[$i]);
        $user_name = $user_name_array[$i];
        $way_desc = $way_type_array[$i];
        $res_id = $res_template_array[$i];
        $new_value = $res_value_array[$i];
        $out = "$time_stamp\t$user_name\t$way_desc\t$res_id\t$new_value";
        fwrite($out_log_file, $out."\n");
    }
}

fclose($out_log_file);


输入文件内容LIKE THIS:

ITEM|1447840806|20090000000016|1666|1103||5125|-10|5|
RES|1447840939|20090000000033|0041|5||110|1|17|
ITEMOVERFLOW|1447842442|20090000000016|1666|1202||16|1|

输出文件内容LIKE THIS:

2015-11-18 13:57:37 3333 2 5106 100 900
2015-11-18 13:57:37 3333 2 5106 99 999
2015-11-18 14:00:01 3333 1002 203 -10 961
2015-11-18 14:00:14 3333 1003 221 -5 994
2015-11-18 14:00:19 3333 1002 203 -89 872
2015-11-18 15:54:04 1111 1002 203 -1 228
2015-11-18 15:54:04 1111 1002 203 -1 227
2015-11-18 15:54:05 1111 1002 203 -1 226
2015-11-18 15:54:05 1111 1002 203 -71 155
2015-11-18 15:58:34 1111 1103 5128 -10 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值