New Posts  All Forums:Forum Nav:

PHP Help Needed

post #1 of 7
Thread Starter 
I just started working with PHP and I'm trying to learn how to pull information out of text files. The project I'm trying to complete involves taking certain information out of a text file which is a rift generated combat log. After googling around for awhile I've found some cool things such as preg_match but I'm not sure if that would work for my situation.

This is the combat log

CombatLog.txt 8k .txt file

What I need out of the file:
-Example for second line of the text document-
Time-18:50:56
Action number: 23
Character name: Udoelerhard
Target name: Boss Practice Dummy
Damage or healing dealt: 1048


Any suggestions are welcome smile.gif. Thanks!thumb.gif
post #2 of 7
Give me a few minutes here! smile.gif
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #3 of 7
Here you go!
Code:
<?php
$FILE_PATH = "CombatLog.txt";

final class DataObject
{
        public $Time;           /* Time */
        public $Action;         /* Action number */
        public $Character;      /* Character name */
        public $Target;         /* Target name */
        public $Entropy;                /* Damage or healing dealt */
        public $Comment;                /* Text comment data */
}

class FileParser
{
        public static function ParseFileIntoLines( $filePath )
        {
                $lines = file( $filePath );

                if ( !$lines )
                {
                        echo 'Failed to parse the file.';
                        exit;
                }
        
                return $lines;
        }

        public static function ParseLinesToDataObjects( $lines )
        {
                if ( !is_array( $lines ) )
                {
                        trigger_error( '$lines was not a valid array', E_USER_ERROR );
                        return NULL;
                }

                $dataObjects = array();
                $tmp = NULL;

                foreach ( $lines as $line )
                {
                        $tmp = self::GetDataObject( $line );

                        if ( $tmp )
                        {
                                $dataObjects[] = $tmp;
                        }
                }

                return $dataObjects;
        }

        public static function GetDataObject( $line )
        {
                if ( $line )
                {
                        $data = new DataObject();

                        /* Get the first paren '(' from the string */
                        $position = stripos( $line, '(' );

                        if ( $position === FALSE )
                        {
                                return NULL;                    
                        }

                        /* All data before the first paren is the time */
                        $data->Time = rtrim( trim( substr( $line, 0, $position ) ), ':' );

                        /* Get the position of the last paren ')' so we know what we're parsing here */
                        $lastPosition = strripos( $line, ')' );

                        if ( $lastPosition === FALSE )
                        {
                                return NULL;
                        }

                        /* Get the text between the parens */
                        $inner = trim( substr( $line, $position + 1, $lastPosition - 1 ) );

                        /* Condense the commas */
                        $inner = str_replace( ' , ', ',', $inner );

                        /* Split the data by the commas */
                        $split = explode( ',', $inner ); 

                        $data->Action        = ( int ) $split[0];
                        $data->Character     = $split[5];
                        $data->Target        = $split[6];
                        $data->Entropy       = ( int ) $split[7];

                        /* All data after the last paren is the 'comment' */
                        $data->Comment = trim( substr( $line, $lastPosition + 1 ) );

                        return $data;
                }

                return NULL;
        }
}

/******************************************
* USAGE 
*******************************************/
$lines = FileParser::ParseFileIntoLines( $FILE_PATH );
$data  = FileParser::ParseLinesToDataObjects( $lines );

var_dump( $data );
?>

All done without regex... where possible I like to avoid it as code will be more performant. I am more than happy to explain everything to you as well!
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #4 of 7
Thread Starter 
Wow thanks a lot! I'll post what I don't understand:

Line 40: What are you comparing in the foreach?
Line 42: I'm not sure what the "self::" does.
Line 71: Are you just moving the file pointer?
Line 105&106: Are you calling two methods at once with the "::" or what does that do?

Everything else I think I can follow decently. It helps that you made your code nice and pretty thumb.gif
post #5 of 7
Quote:
Originally Posted by Moheevi_chess View Post

Wow thanks a lot! I'll post what I don't understand:
Line 40: What are you comparing in the foreach?
Line 42: I'm not sure what the "self::" does.
Line 71: Are you just moving the file pointer?
Line 105&106: Are you calling two methods at once with the "::" or what does that do?
Everything else I think I can follow decently. It helps that you made your code nice and pretty thumb.gif

Line 40: Not comparing anything, I am looping through every line in the file smile.gif
Line 42: Instructs PHP to call the method "GetDataObject" in the current class, which is "FileParser". "self" is essentially the static analogy to a "this" pointer in C++ (and indeed PHP too) when in object scope. My class is in static scope.
Line 71: At this point we're not working with any file pointers at all as I have loaded the entire file into memory. That line simply gets the last position of the ")" character of the file line that was passed to the function.
Line 105&106: Instructs PHP to call the method "ParseFileIntoLines" in the static class "FileParser", the same for "ParseLinesToDataObjects".

P.S. you can look up the functions I call in the PHP manual, they may help you to understand too! Or keep posting here wink.gif

No worries... I believe the forum software is mostly responsible for making the code pretty thumb.gif !
Edited by tompsonn - 6/23/12 at 7:20am
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #6 of 7
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

Line 40: Not comparing anything, I am looping through every line in the file smile.gif
Line 42: Instructs PHP to call the method "GetDataObject" in the current class, which is "FileParser". "self" is essentially the static analogy to a "this" pointer in C++ (and indeed PHP too) when in object scope. My class is in static scope.
Line 71: At this point we're not working with any file pointers at all as I have loaded the entire file into memory. That line simply gets the last position of the ")" character of the file line that was passed to the function.
Line 105&106: Instructs PHP to call the method "ParseFileIntoLines" in the static class "FileParser", the same for "ParseLinesToDataObjects".
P.S. you can look up the functions I call in the PHP manual, they may help you to understand too! Or keep posting here wink.gif
No worries... I believe the forum software is mostly responsible for making the code pretty thumb.gif !

Very cool! My end goal is to have a very simple website site, just for me, where I can select a fight from rift and see my damage per second/ healing per second on that boss.

I think this works perfectly, I'm going to explode and implode the necessary $Data->Time strings to create a $totalTime integer.

I have my database set up so all I need to do now is theory craft some php code:D
post #7 of 7
Quote:
Originally Posted by Moheevi_chess View Post

Very cool! My end goal is to have a very simple website site, just for me, where I can select a fight from rift and see my damage per second/ healing per second on that boss.
I think this works perfectly, I'm going to explode and implode the necessary $Data->Time strings to create a $totalTime integer.
I have my database set up so all I need to do now is theory craft some php code:D

Sounds good! Check out the strtotime function: http://www.php.net/manual/en/function.strtotime.php
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming