Jump to content

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

 

:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.