Parsing a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kingnothing737
    New Member
    • Feb 2007
    • 11

    Parsing a text file

    Alright. The text file in question is an online game server's log file (log.log being the file, the game being Halo). It has information in it that I would like to be extracted and put in a table in a php file. It needs to be polled regularly (maybe every 30 minutes). Here is a piece of the log file:

    Code:
    2007-02-16 21:58:42	KILL	"¬ÞØøþ¬"	player 3	KILLED	"MastaSpoofa"	player 5
    2007-02-16 21:58:43	CHAT	GLOBAL 	[HIV]Cheefa	what map
    2007-02-16 21:58:44	CHAT	GLOBAL 	MastaSpoofa	LOL
    2007-02-16 21:58:46	CHAT	GLOBAL 	¶®ïñ©eÒf¶wñ	lol on the gun
    2007-02-16 21:58:52	CHAT	GLOBAL 	MastaSpoofa	blood gulch
    2007-02-16 21:58:53	CHAT	GLOBAL 	MastaSpoofa	go
    2007-02-16 21:59:00	CHAT	GLOBAL 	MastaSpoofa	ice fields
    2007-02-16 21:59:00	CHAT	GLOBAL 	MastaSpoofa	go
    2007-02-16 21:59:04	SCORE	CTF	player 4	"¬ÞØøþ¬"	team 0
    2007-02-16 21:59:04	PCR_TEAM	Team 0	Score	3
    2007-02-16 21:59:04	PCR_TEAM	Team 1	Score	0
    2007-02-16 21:59:04	PCR_PLAYER	Place 1	Player 4 "¬ÞØøþ¬"	Team 0	Kills 6	Assists 1	Deaths 5	Score 2
    2007-02-16 21:59:04	PCR_PLAYER	Place 2	Player 2 "pikachu"	Team 0	Kills 5	Assists 2	Deaths 4	Score 1
    2007-02-16 21:59:04	PCR_PLAYER	Place 3	Player 6 "MastaSpoofa"	Team 1	Kills 6	Assists 0	Deaths 4	Score 0
    2007-02-16 21:59:04	PCR_PLAYER	Place 4	Player 5 "[HIV]Cheefa"	Team 1	Kills 4	Assists 2	Deaths 3	Score 0
    2007-02-16 21:59:04	PCR_PLAYER	Place 5	Player 3 "Isaac"	Team 0	Kills 3	Assists 2	Deaths 5	Score 0
    2007-02-16 21:59:04	PCR_PLAYER	Place 6	Player 1 "¶®ïñ©eÒf¶wñ"	Team 1	Kills 2	Assists 3	Deaths 9	Score 0
    The information I want to extract are in the lines with "PCR_TEAM" and "PCR_PLAYER ," and I want all of the information in those lines which comes after the aforementioned strings to be displayed in tables in a php file (and maybe the date and time on the line, as well). They contain the scores for each team and each player at the end of every game. There are several instances of these in the log file, and I would like each instance to have its own table.

    Here's an example of how I would like the information to be displayed in a table:

    Code:
    2007-02-16 
    21:59:04
    
    Place	Name		Team	Kills	Assists	Deaths	Score
    
    1	¬ÞØøþ¬		0	6	1	5	2
    2	pikachu		0	5	2	4	1
    3	MastaSpoofa	1	6	0	4	0
    4	[HIV]Cheefa	1	4	2	3	0
    5	Isaac		0	3	2	5	0
    6	¶®ïñ©eÒf¶wñ	1	2	3	9	0
    ------------------------------------------------------------------
    	Team 0						3
    	Team 1						0
    Would someone be kind enough to write a php file which will do that? I know next to nothing about php, so the code for this would need to be complete (as opposed to being just a certain part of the file) for me to be able to utilize it.
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    Is there a way a line of the log can have properly seperated columns? now there are columns with five/four spaces in between and other with only one.

    This will be needed to extract the data from every line.

    Comment

    • kingnothing737
      New Member
      • Feb 2007
      • 11

      #3
      Originally posted by xwero
      Is there a way a line of the log can have properly seperated columns? now there are columns with five/four spaces in between and other with only one.

      This will be needed to extract the data from every line.
      The deliminators used are tabs. I *think* that's what you're wanting to know.

      Comment

      • xwero
        New Member
        • Feb 2007
        • 99

        #4
        Originally posted by kingnothing737
        The deliminators used are tabs. I *think* that's what you're wanting to know.
        i'm at work now but i will try to provide you with some code you need this evening.

        Comment

        • kingnothing737
          New Member
          • Feb 2007
          • 11

          #5
          Originally posted by xwero
          i'm at work now but i will try to provide you with some code you need this evening.
          Thanks, I appreciate it. :)

          Comment

          • xwero
            New Member
            • Feb 2007
            • 99

            #6
            ok here we go :)

            [PHP]
            // fetches log files in an array per line
            $lines = file('http://example.com/halo.log');
            // two arrays to catch the things you need
            $teams = array();
            $players = array();
            // fill the arrays
            foreach($lines as $line){
            if(preg_match(' PCR_TEAM',$line )){ $teams[] = $line; }
            if(preg_match(' PCR_PLAYER',$li ne)){ $players[] = $line; }
            }
            [/PHP]

            now whe have to get all the data out of the arrays

            [PHP]
            // get date from one of the arrays
            $temp = $teams[0];
            $temparr = split(' ',$temp);
            $date = $temparr[0];
            $time = $temparr[0];
            // get teamdata out the team array
            $teamnames = array();
            $teamscore = array();
            foreach($teams as $team){
            $teamarr = split(' ',$team);
            // this is a guess if the output isn't right adjust the numbers
            $teamnames[] = $teamarr[3].' '.$teamarr[4];
            $teamscore[] = $teamarr[6];
            }
            // get playerdata out of playes array
            $playerplace = array();
            $playername = array();
            $playerteam = array();
            $playerkills = array();
            $playerassists = array();
            $playerdeaths = array();
            $playerscore = array();
            foreach($player s as $player){
            $playerarr = split(' ',$player);
            // this is a guess if the output isn't right adjust the numbers
            $playerplace[] = $playerarr[4];
            $playername[] = $playerarr[7];
            $playerteam[] = $playerarr[9];
            $playerkills[] = $playerarr[11];
            $playerassists[] = $playerarr[13];
            $playerdeaths[] = $playerarr[15];
            $playerscore[] = $playerarr[17];
            }
            [/PHP]

            and the final output

            [PHP]
            <p><?php echo $date; ?><br><?php echo $time; ?></p>
            <table>
            <tr><th>place </th><th>name</th><th>and so on</th></tr>
            <?php
            for($i = 0;$i<count($pla yerplace);$i++; ){
            echo '<tr><td>'.$pla yerplace[$i].'</td><td>'.$playe rname[$i].'</td><td>and so on</td></tr>';
            }
            for($i=0;$i<cou nt(teamnames);$ i++){
            // change colspan if needed
            echo '<tr><td colspan="4">'.$ teamnames[$i].'</td><td>'.$teams core.'</td></tr>';
            }
            ?>
            </table>

            [/PHP]

            Comment

            • kingnothing737
              New Member
              • Feb 2007
              • 11

              #7
              Thanks. I'm getting this error, though:

              Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 17

              Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 19


              It keeps repeating that over and over.

              Comment

              • xwero
                New Member
                • Feb 2007
                • 99

                #8
                Originally posted by kingnothing737
                Thanks. I'm getting this error, though:

                Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 17

                Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 19


                It keeps repeating that over and over.
                sorry about that

                [PHP]
                foreach($lines as $line){

                if(preg_match(' PCR_TEAM',$line )){ $teams[] = $line; }

                if(preg_match(' PCR_PLAYER',$li ne)){ $players[] = $line; }

                }
                [/PHP]

                should be

                [PHP]
                foreach($lines as $line){

                if(preg_match('/PCR_TEAM/',$line)){ $teams[] = $line; }

                if(preg_match('/PCR_PLAYER/',$line)){ $players[] = $line; }

                }
                [/PHP]

                Comment

                • kingnothing737
                  New Member
                  • Feb 2007
                  • 11

                  #9
                  All it's outputting is this:

                  "place name and so on
                  Array"

                  :(

                  Comment

                  • xwero
                    New Member
                    • Feb 2007
                    • 99

                    #10
                    Originally posted by kingnothing737
                    All it's outputting is this:

                    "place name and so on
                    Array"

                    :(
                    what does it output when you do

                    [PHP]
                    // after the first code block
                    print_r($teams) ;
                    // after the seccond code block
                    print_r($teamna mes);
                    [/PHP]

                    Comment

                    • kingnothing737
                      New Member
                      • Feb 2007
                      • 11

                      #11
                      "Array ( ) Array ( )


                      place name and so on
                      Array"

                      Comment

                      • xwero
                        New Member
                        • Feb 2007
                        • 99

                        #12
                        have you got the right link to your file?

                        Comment

                        • kingnothing737
                          New Member
                          • Feb 2007
                          • 11

                          #13
                          Originally posted by xwero
                          have you got the right link to your file?
                          Yeah, it's right.

                          Comment

                          • xwero
                            New Member
                            • Feb 2007
                            • 99

                            #14
                            Originally posted by kingnothing737
                            Yeah, it's right.
                            what gives

                            [PHP]print_r($lines) ;[/PHP]

                            as output?

                            Comment

                            • kingnothing737
                              New Member
                              • Feb 2007
                              • 11

                              #15
                              Originally posted by xwero
                              what gives

                              [PHP]print_r($lines) ;[/PHP]

                              as output?
                              Ok, that worked. It output the entire log file.

                              Edit: the line breaks are messed up, though.

                              Comment

                              Working...