Jump to content

Parse file with PHP


cat250

Recommended Posts

Hei there.

 

I have this in file.log

L 02/10/2012 - 15:50:56: Log file started (file "cstrike\addons\amxmodx\logs\ban_history.log") (game "cstrike") (amx "1.8.1.3746")
L 02/10/2012 - 15:03:38: ABC <STEAM_ID_LAN> banned User1 <284.121.146.100> || Reason: "Reason 1" || Ban Length: 1 minute
L 02/10/2012 - 15:17:24: Ban time is up for: User1 [284.121.146.100]
L 02/10/2012 - 15:17:39: DEF <STEAM_ID_LAN> banned User2 <234.151.143.110> || Reason: "Reason 2" || Ban Length: 1 minute
L 02/10/2012 - 15:18:39: Ban time is up for: User2 [234.151.143.110]

 

And I want to show in page only:

ABC - User 1 <284.121.146.100> - Reason 1 - 1 minute
DEF - User 2 <234.151.143.110> - Reason 2 - 1 minute

 

How can I do this? Thanks.

Link to comment
Share on other sites

You would need to identify what is unique about the lines in the log file that you want. Will they only be the lines with the || separator or will they only be the lines with the Reason: text? Or perhaps both of those? Then you would probably want to use preg_match_all to find and extract just those portions of the lines that you want.

Link to comment
Share on other sites

You would need to identify what is unique about the lines in the log file that you want. Will they only be the lines with the || separator or will they only be the lines with the Reason: text? Or perhaps both of those? Then you would probably want to use preg_match_all to find and extract just those portions of the lines that you want.

 

Thanks for reply. I looked over preg_math_all, but still don't know how can i do this. :-\

Link to comment
Share on other sites

Well...what's unique about that line?  Regex is a difficult language, but it's not impossible.  However, we're not going to write you something so you can come back and say "I tried that, and it pulled the lines I wanted, but also these 17 other kinds of lines that I don't want but never told you about."  Tell us what's unique about that line, and you can have a regex that will match it

Link to comment
Share on other sites

Well...what's unique about that line?  Regex is a difficult language, but it's not impossible.  However, we're not going to write you something so you can come back and say "I tried that, and it pulled the lines I wanted, but also these 17 other kinds of lines that I don't want but never told you about."  Tell us what's unique about that line, and you can have a regex that will match it

 

Hmm, || it's unique. Also, I need to remove "Reason: " and "Ban Length: ". But it's hard for me, never worked with this. From this L 02/10/2012 - 15:17:39: DEF <STEAM_ID_LAN> banned i need only "DEF".

It seems I need only 2, 4, 6, 8 lines etc. If u understand what i say, bad english here. :-\

Will no be "17 other kinds of lines", i put the exact code. Thanks. :confused:

Link to comment
Share on other sites

With this code, $foo[1] will be an array of the items you said you wanted:

 

$file = 'L 02/10/2012 - 15:50:56: Log file started (file "cstrike\addons\amxmodx\logs\ban_history.log") (game "cstrike") (amx "1.8.1.3746")
L 02/10/2012 - 15:03:38: ABC <STEAM_ID_LAN> banned User1 <284.121.146.100> || Reason: "Reason 1" || Ban Length: 1 minute
L 02/10/2012 - 15:17:24: Ban time is up for: User1 [284.121.146.100]
L 02/10/2012 - 15:17:39: DEF <STEAM_ID_LAN> banned User2 <234.151.143.110> || Reason: "Reason 2" || Ban Length: 1 minute
L 02/10/2012 - 15:18:39: Ban time is up for: User2 [234.151.143.110]';

preg_match_all('/(\w+)\s*<STEAM_ID_LAN>\s*banned/', $file, $foo);
echo "Banned: " . implode(", ", $foo[1]);
//Banned: ABC, DEF

Use file_get_contents to get a file off the hard drive into the $file variable

 

-Dan

Link to comment
Share on other sites

With this code, $foo[1] will be an array of the items you said you wanted:

 

$file = 'L 02/10/2012 - 15:50:56: Log file started (file "cstrike\addons\amxmodx\logs\ban_history.log") (game "cstrike") (amx "1.8.1.3746")
L 02/10/2012 - 15:03:38: ABC <STEAM_ID_LAN> banned User1 <284.121.146.100> || Reason: "Reason 1" || Ban Length: 1 minute
L 02/10/2012 - 15:17:24: Ban time is up for: User1 [284.121.146.100]
L 02/10/2012 - 15:17:39: DEF <STEAM_ID_LAN> banned User2 <234.151.143.110> || Reason: "Reason 2" || Ban Length: 1 minute
L 02/10/2012 - 15:18:39: Ban time is up for: User2 [234.151.143.110]';

preg_match_all('/(\w+)\s*<STEAM_ID_LAN>\s*banned/', $file, $foo);
echo "Banned: " . implode(", ", $foo[1]);
//Banned: ABC, DEF

Use file_get_contents to get a file off the hard drive into the $file variable

 

-Dan

 

Thank u so much. Now I've tried to show User2 <234.151.143.110> - Reason 2 - 1 minute but don't get it.

I need to edit regular expression? I've tried to use foo to show other items but no succes.

 

Thanks again.

Link to comment
Share on other sites

This is exactly the point of my previous post.  I asked you to tell me what you wanted, and you said "I want only the DEF."  That's what you got.

 

This code matches your original output:

<?php
$file = 'L 02/10/2012 - 15:50:56: Log file started (file "cstrike\addons\amxmodx\logs\ban_history.log") (game "cstrike") (amx "1.8.1.3746")
L 02/10/2012 - 15:03:38: ABC <STEAM_ID_LAN> banned User1 <284.121.146.100> || Reason: "Reason 1" || Ban Length: 1 minute
L 02/10/2012 - 15:17:24: Ban time is up for: User1 [284.121.146.100]
L 02/10/2012 - 15:17:39: DEF <STEAM_ID_LAN> banned User2 <234.151.143.110> || Reason: "Reason 2" || Ban Length: 1 minute
L 02/10/2012 - 15:18:39: Ban time is up for: User2 [234.151.143.110]';

preg_match_all('/(\w+)\s*<STEAM_ID_LAN>\s*banned\s*(.+?) \|\| Reason: "([^"]+)" \|\| Ban Length: (.+)/m', $file, $foo);

echo '<pre>';
foreach ( $foo[1] as $k => $v ) {
  echo "{$v} - {$foo[2][$k]} - {$foo[3][$k]} - {$foo[4][$k]}\n";
}
echo "</pre>';

Link to comment
Share on other sites

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.