JasonO Posted May 13, 2010 Share Posted May 13, 2010 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 More sharing options...
JasonO Posted May 13, 2010 Author Share Posted May 13, 2010 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 Link to comment https://forums.phpfreaks.com/topic/201571-best-way-to-approach-a-parse-script/#findComment-1057509 Share on other sites More sharing options...
kenrbnsn Posted May 13, 2010 Share Posted May 13, 2010 Probably the best way would be to use a regular expression. I can't help you there, since I'm very weak with them. If you post in the PHP Regex sub group someone could probably help. Ken Link to comment https://forums.phpfreaks.com/topic/201571-best-way-to-approach-a-parse-script/#findComment-1057511 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.