cat250 Posted February 10, 2012 Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 10, 2012 Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316569 Share on other sites More sharing options...
cat250 Posted February 10, 2012 Author Share Posted February 10, 2012 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. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316574 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316591 Share on other sites More sharing options...
cat250 Posted February 10, 2012 Author Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316593 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316608 Share on other sites More sharing options...
cat250 Posted February 10, 2012 Author Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316621 Share on other sites More sharing options...
ManiacDan Posted February 10, 2012 Share Posted February 10, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316628 Share on other sites More sharing options...
cat250 Posted February 10, 2012 Author Share Posted February 10, 2012 Thaaaaaanks! Quote Link to comment https://forums.phpfreaks.com/topic/256817-parse-file-with-php/#findComment-1316645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.