Home > Back-end >  Search for next entry in text file based on array value from previous preg_match search
Search for next entry in text file based on array value from previous preg_match search

Time:02-28

Ok so I have a log file that contains info like below

7:04:49 | Player "PLAYER A"(id=BC843EF4A109D1FE413E69FCCD789A2044224192) has been 
disconnected
07:05:03 | Player "PLAYER B" is connected (id=0EFCEFBB9F30A04B0BAA08E97766C368F9652642)
07:05:27 | ##### PlayerList log: 9 players
07:05:27 | Player "PLAYER B" (id=72C79B428E4A6DA8E1A915A20BEC65A3B993B072 pos=<2046.0, 14926, 439.1>)

Now I am searching the log for the player is connected lines This works and heres the code

 $coordpattern = '/^(.*?)Player (.*?) is connected/m';
            if(preg_match_all($coordpattern, $file, $items))
            {
             $logins = array();
             $i = 0;
                foreach($items[$i] as $item=>$val) {
                preg_match('/"([^"] )"/',$val,$player);
                $time = substr($val,0,8);
                $offset = strpos($file,$val);
                //echo "<pre>";
                //echo $time.' - '.$player[0];
                //echo "</pre>";
                $logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);
                $i  ;
                }
            }

This is then building an array containing the Time, Player Name and strpos of the start of the line ( I assume )

What I want to then do is to search the log for any lines after the player is connected that match the player name. I'm trying to check the time from the start of the line and if its greater than the time in the logins array then just echo "found" However its just not returning anything..

Heres my code

foreach($logins as $playername){
    //echo $playername["Player"].'<br>';
    //var_dump($logins);
    //echo $playername[$l]['Player']."<br>";
    $player = str_replace('"','',$playername["Player"]);
    $logintime = $playername["Time"];
        $pospattern = '/^(.*?)Player \"'.$player.'\" \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/';
            if(preg_match_all($pospattern, $file, $positions))
            {
             $s = 0;
                foreach($positions[$s] as $pos=>$val) {
                $login = intval(str_replace(":","",$logintime));
                preg_match('/"([^"] )"/',$val,$player);
                $time = substr($val,0,8);
                $actualtime = intval(str_replace(":","",$time));
                if($actualtime > $login)
                {
                    echo "found";
                }
                $s  ;
                }
            }
        }

CodePudding user response:

Your code is not working, I've rewritten it here:

<?php

$file = <<<FILE
7:04:49 | Player "PLAYER A"(id=BC843EF4A109D1FE413E69FCCD789A2044224192) has been 
disconnected
07:05:03 | Player "PLAYER B" is connected (id=0EFCEFBB9F30A04B0BAA08E97766C368F9652642)
07:05:27 | ##### PlayerList log: 9 players
07:05:27 | Player "PLAYER B" (id=72C79B428E4A6DA8E1A915A20BEC65A3B993B072 pos=<2046.0, 14926, 439.1>)
FILE;

$coordpattern = '/^(.*?)Player (.*?) is connected/m';
if(preg_match_all($coordpattern, $file, $items))
{
 $logins = array();
 $i = 0;
    foreach($items[$i] as $item=>$val) {
    preg_match('/"([^"] )"/',$val,$player);
    $time = substr($val,0,8);
    $offset = strpos($file,$val);
    //echo "<pre>";
    //echo $time.' - '.$player[0];
    //echo "</pre>";
    $logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);
    $i  ;
    }
}
            
foreach($logins as $playerData){
        $playername = $playerData["Player"];
        $logintime = $playerData["Time"];
        
        $pospattern = '/(.*?)Player '.$playername.' \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/';

        if(preg_match_all($pospattern, $file, $positions))
        {
            $s = 0;
            foreach($positions[$s] as $pos=>$val) {
                $login = intval(str_replace(":","",$logintime));
                preg_match('/"([^"] )"/',$val,$player);
                $time = substr($val,0,8);
                $actualtime = intval(str_replace(":","",$time));
                if($actualtime > $login) {
                    echo "found";
                }
                $s  ;
            }
        }
    }

There were two issues. Logins is an array of arrays. The foreach in your original post has it as $logins[1] which will never work.

Then, when you're assigning the player name with $logins[] = array("Time"=>$time,"Player"=>$player[0],"offset"=>$offset);, "Player" actually contains the double quotes as well. So your regex was like: "/(.*?)Player ""PLAYER B"" \(id=(.*?) pos=<(.*?),(.*?),(.*?)\)/"

There is no entry for ""PLAYER B"". Removing the extra double quote from your $posplayer would fix it.

Try to debug the regex pattern next time. Whenever you extract something with regex and use it again, always dump the concatenation result.

  • Related