Jump to content

Best way to approach a parse script


JasonO

Recommended Posts

Hi there,

I have a log file which has lines like the following:

 

22:18:09 USER Tom.D connecting.
22:18:16 geo987 INCLUDES FILES - CA;@ACE;@add1
22:18:20 USER Jason connected (id=30537990).

 

The lines I'm interested with is the one containing the users ID.. There are many other lines in the log as well, however its only the connected one I want to worry about.

 

What would be the best way to approach this so I can get the Username and ID from these lines into seperate variables. The username can contain symbols and the id could be a length of anything from 4 digits to 15+; which is what I'm finding an issue.

 

Thank you for your suggestions,

Jason

Link to comment
https://forums.phpfreaks.com/topic/201571-best-way-to-approach-a-parse-script/
Share on other sites

Well, I got something put together. This is what I have.

 

Can you guys suggest a better way or point out any pottential errors? I've thrown thousands of lines of the log through it and no issues so far.

 

$logData = $_POST['logData'];
$entries = explode("\n", $logData);
?>

<table style="border: 1px solid #000000; border-collapse: collapse; width: 240px;">
        <tr>
            <td style="border: 1px solid #000000; border-collapse: collapse; padding: 4px;"><b>Player Name</b></td>
            <td style="border: 1px solid #000000; border-collapse: collapse; padding: 4px;"><b>Player ID</b></td>
        </tr>
<?php
foreach ($entries as $i => $entry) {
	if(!strpos($entry, "connected (id="))
	{	
		// The line doesnt refer to 'connected' thus not relevent here
		unset($entries[$i]);
	}
	else
	{	
		 echo "<tr >";
		 // A basic strip down of stuff we always never need
		 $newEntry = substr_replace($entry, "", 0, 16);
		 $entries[$i] = $newEntry;

		 // Take out the Player Name
		 $playerName = substr($newEntry, 0, strpos($newEntry, "connected (id="));

		 // Take out the Player ID
		 $playerID = strstr ($newEntry, "connected (id=");
		 $playerID = substr_replace($playerID, "", 0, 14);
		 $playerID = ereg_replace("[^0-9]", "", $playerID );

		 // Print into our table row
		 echo "<td style=\"border: 1px solid #000000; border-collapse: collapse; padding: 4px;\">".$playerName."</td><td style=\"border: 1px solid #000000; border-collapse: collapse; padding: 4px;\">".$playerID."</td>";

		 echo "</tr>";

	}
}

echo "</table>";

 

Turns results into a table like:

 

Name

ID

Jason

1234567

Tom

9512312

 

:)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.