Splitting a log file in php

Discussion in 'PHP' started by sandstorm140, Oct 6, 2008.

  1. #1
    Using an uploader form,
    Is it possible to upload and use the following style txt file:

    ---------------
    <name1> I have this error running from inside google
    <name1> name3 you know what address I've passed you last friday?
    <name2> hi. is there a COMMAND LINE svg editor for linux?
    <name2> i explain my problem
    <name3> no name1 :(
    <name1> xslt processors? custom scripts perhaps?
    <name4> but nothing out-of-the-box that I know of
    ----------------

    and extract the names between < > and display each name's string count over all in the entire log file?

    for example:
    echo ('name1 = 209 characters typed');
    echo ('name2 = 100 characters typed');
    so on..

    echo ('over all characters type X');
    echo ('over X amount of users');

    I basically want my existing IRC log file viewed as the example above.
    thanks in advanced.
     
    sandstorm140, Oct 6, 2008 IP
  2. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, it is possible. I could write it if you wanted?
     
    NatalicWolf, Oct 6, 2008 IP
  3. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes please, whenever you have time, I would like to learn how to do it, i'd greatly appreciate it. Thank you :)
     
    sandstorm140, Oct 7, 2008 IP
  4. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #4
    Here is the code..

        $myfile = ‘http://site.com/log/2.log’;
        $file_handle = fopen(”http://site.com/log/2.log”, “r”);
        $i=0;
        echo”<pre>”;
    
        //i is number of lines to read
    
        while (!feof($file_handle)&&($i<8000)) {
    
        $line = fgets($file_handle);
    
        //prints the line
    
        echo $line;
    
        echo”<BR>”;
    
        $i=$i+1;
        }
        echo”</pre>”;
        fclose($file_handle);
        ?>
    PHP:
     
    Freewebspace, Oct 7, 2008 IP
  5. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks freewebspace, that isn't exactly what i'm needing
     
    sandstorm140, Oct 7, 2008 IP
  6. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?PHP
    $f=fopen('file.txt','r');//Read
    while(($data=fgets($f))!==false)
    {
    /* Preg_Match matches one time to a regular expression(perl)
    using preg_match_all finds multiple of the expression, but for this, we need it once.

    Expression explanation:
    ^ - Starts the expression -- must end with this too
    \< - since < is a command in regex, we escape it this is the < of <name>
    (.*) - Finds all data
    \> -- Same as the \<
    (space) - the space in the strings
    (.*) - same as above, gets all data.
    ^ - End the expression
    */
    @preg_match('^\<(.*)\> (.*)^',$data,$match);
    if($match[1]!='')
    $array[$match[1]]+=strlen($match[2]);
    }
    /*
    foreach ($ArrayVariable as $KeyName => $KeyValue)

    Example:
    Our array has keys, they are the usernames
    Foreach loops through that array, each key is the "as $Keyname" the variable maybe named anything, but use something relavent for sanity sakes.
    Then the "=> $KeyValue" gives you the value of the key in the variable $KeyValue. Once again, this can be anything, but use relavent names */

    foreach($array as $name => $number)
    {
    echo $name.' typed '.$number.' letters in this chat<br />';
    }
    fclose($f)
    ?>
     
    NatalicWolf, Oct 8, 2008 IP
  7. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You explained that so well lol, i couldn't ever thank you enough.
     
    sandstorm140, Oct 8, 2008 IP
  8. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I have made a few slight changes and am having problems... But not with things I changed lol:

    <?PHP
    
    $file = 'log.txt'; //File to open
    $f=fopen($file,'r');//Read
    
    while(($data=fgets($f))!==false)
    {
    	@preg_match("^\<(.*)\> (.*)^",$data,$match);
    	
    	if($match[1]!='')
    		$array[$match[1]]+=strlen($match[2]);
    }
    foreach($array as $name => $number)
    {
    	$html = htmlentities($name.' typed '.$number.' chars');
    	echo $html.'<br />';
    }
    fclose($f)
    
    ?>
    PHP:
    I'm having one simple problem.
    Log file contains:
    
    <Name1> hi
    <Name2> !ghelp yourki
    <+GoogleBot> Name2: <Name2> what is yourki?
    <Name4> hiya Name1
    
    Code (markup):
    I get an output as:
    
    Name1 typed 2 chars
    Name2 typed 14 chars
    +GoogleBot> Name2: <Name2 typed 18 chars
    Name4 typed 10 chars
    
    Code (markup):
    It should just give me an output of:
    
    Name1 typed 2 chars
    Name2 typed 14 chars
    +GoogleBot typed 32 chars
    Name4 typed 10 chars
    
    Code (markup):
    I think the problem is the +GoogleBot inserts <Name2> into the log and when the script runs, it gets confused at that line.
    Thanks in advanced for any help :).
     
    sandstorm140, Oct 8, 2008 IP
  9. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Solved, replaced:
    @preg_match("^\<(.*)\> (.*)^",$data,$match);
    with
    @preg_match("^\<(.*?)\> (.*)^",$data,$match);

    a pesky little question mark lol
     
    sandstorm140, Oct 9, 2008 IP