关闭

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

221人阅读 评论(0) 收藏 举报
分类:
#!/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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:64721次
    • 积分:1114
    • 等级:
    • 排名:千里之外
    • 原创:43篇
    • 转载:13篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论