过滤日志PHP

时间:2016-01-04 07:30:22

标签: php json

我正在研究PHP上的代码,该代码从日志中读取行并根据所需日期回显给用户。

这是我的代码,

<?php
$x=0;

$filestring= file_get_contents( "argh.txt" ); // get the contents, and echo it out.
$filearray = explode("/n", $filestring);

    while (list($var,$val) = each($filearray)) {
        print "$val <br/>";
        ++$x;
    }
?>

这是我的输出:

00:31:32,subID=1,SC=22919,Ctnt=Talk
00:31:45,subID=1,SC=22919,Ctnt=ON
00:31:45,subID=2,SC=22919,Ctnt=INVITE 0196966467
00:31:45,subID=1,SC=22919,Ctnt=Alright
00:31:45,subID=2,SC=22919,Ctnt=INVITE 0146966467
00:31:45,subID=2,SC=22919,Ctnt=INVITE 0166966467

我希望结果基于subID和Ctnt = Invite显示(过滤并显示结果,sub id可以是任何东西,但Ctnt必须根据invite 0 *进行过滤) 结果, 如 : subID = 1,Ctnt =

1 个答案:

答案 0 :(得分:0)

以下是您提供的代码中的内容,请尝试一下:

<?php
$x=0;
$filter_subID = $_POST['subID'];
$filter_Ctnt = $_POST['Ctnt'];

$filestring= file_get_contents( "argh.txt" ); // get the contents, and echo it out.
$filearray = explode("/n", $filestring);

    while (list($var,$val) = each($filearray)) {
        $match_sub = false;
        $match_ctnt = false;
        $csv = explode(',',$val);
        unset($csv[0]);
        $csv = array_values($csv);
        foreach($csv as $logvals){
            list($field,$value) = explode('=',$logvals);
            if(trim($field) == 'subID' && trim($value) == $filter_subID){
                $match_sub = true;
            }
            if(trim($field) == 'Ctnt' && trim($value) == $filter_Ctnt){
                $match_ctnt = true;
            }
        }
        if($match_sub && $match_ctnt) print "$val <br/>";
        ++$x;
    }
?>