Jump to content

Breaking down a String


creiss

Recommended Posts

Hey folks!

 

I have like 4000 logfiles that I would like to push over into a database. The loglines are like this

 

[12.10.2005 14:34:32] httpd: started http daemon.

 

So in a more general view:

[date] descriptor: text

 

Now, there are space-delimiters, and the only way to split up the string is the "[" and "]" char, as well as the ":" char, and everything else that follows.

 

I'd like to have an array/ 3 vars like this

 

$date = datestamp w/o the "[" and "]",

$desc = descriptor, everything bewteen the "] " (note the exclusion of the first space after the "]" char) and the ":" char,

and

$text = everything from ":" to end.

 

each line in the log is a new entry, there are no multi-line entries.

 

Anyone could help me with splitting that bastard up?

 

Thank you all in advance,

Merry (remaining) Christmas,

-Chris.

Link to comment
Share on other sites

use substr() to strip the date and time from the string

 

$test = "[12.10.2005 14:34:32] httpd: started http daemon.";

echo substr($test, 0, 19); // Should return "12.10.2005 14:34:32"

 

As for the rest I'm going to assume that they are going to be of varying length so you cant know exactly when and where they begin and end.

 

I'll get back to you on the rest, but it's a start ;)

Link to comment
Share on other sites

use substr() to strip the date and time from the string

 

$test = "[12.10.2005 14:34:32] httpd: started http daemon.";

echo substr($test, 0, 19); // Should return "12.10.2005 14:34:32"

 

As for the rest I'm going to assume that they are going to be of varying length so you cant know exactly when and where they begin and end.

 

I'll get back to you on the rest, but it's a start ;)

 

And what if the date is malformed, as in am:

$test = "[12.10.2005 4:34:32] httpd: started http daemon.";

 

Meh :)

 

-Chris.

Link to comment
Share on other sites

for the rest you could just do this:

(im taking the first part from drummer101)

 

$test = "[12.10.2005 14:34:32] httpd: started http daemon.";

$nobracket = substr($test, 0, 19); // Should return "12.10.2005 14:34:32"

$take_space = explode(' ', $nobracket);

$one = explode(".", $take_space[0]);

$two = explode(":", $take_space[1]);

 

foreach($one as $n => $date){

echo $date . "<br>\n";

}

echo "<br>\n";

foreach($two as $no => $time){

echo $time . "<br>\n";

}

 

the above code should return

 

12

10

2005

 

14

34

32

Link to comment
Share on other sites

Here's my solution using the explode() function:

<?php
function breakstring($str) {
list ($dmy,$str1) = explode('[',$str);
list ($date,$tmp) = explode(']',$str1);
list ($desc,$text) = explode(':',$tmp);
$desc = trim($desc);
$text = trim($text);
return(array($date,$desc,$text));
}

$str = '[12.10.2005 14:34:32] httpd: started http daemon.';
list ($date,$desc,$text) = breakstring($str);
echo 'Original string: ' . $str . "<br>\n";
echo 'Date: ' . $date . "<br>\n";
echo 'Desc: ' . $desc . "<br>\n";
echo 'Text: ' . $text . "<br>\n";
?>

 

Ken

Link to comment
Share on other sites

use substr() to strip the date and time from the string

 

$test = "[12.10.2005 14:34:32] httpd: started http daemon.";

echo substr($test, 0, 19); // Should return "12.10.2005 14:34:32"

 

As for the rest I'm going to assume that they are going to be of varying length so you cant know exactly when and where they begin and end.

 

I'll get back to you on the rest, but it's a start ;)

 

And what if the date is malformed, as in am:

$test = "[12.10.2005 4:34:32] httpd: started http daemon.";

 

Meh :)

 

-Chris.

What I posted assumed you were using mm/dd/yyyy hh:mm:ss

 

Your answer was the exception :)

 

position 0, 19 in that case would be "12.10.2005 4:34:32]" and from there you could use str_replace()

$text = "[12.10.2005 4:34:32] httpd: started http daemon.";

$strip = "]";

echo str_replace(substr($text, 0, 19)); // Should return 12.10.2005 4:34:32

 

You could also use

$strip = array("[", "]");

$text = "[12.10.2005 4:34:32] httpd: started http daemon.";

$sub = substr($text, 0, 20);

$date = str_replace($strip, "", $sub);

echo $date;

 

Edit: I like Ken's answer better :D

Link to comment
Share on other sites

My 0.02 worth

 

<?php 

function parseStr($str)
{
    $k = strlen($str);
    $data = array();
    $i = 0;

    for ($j=0; $j<$k; $j++)
    {
        switch ($c = $str[$j])
        {
            case '[':
                break;
            case ']':
                $i++;
                $j++;
                break;
            case ':':
                if ($str[$j+1] == ' ') {
                    $i++;
                    $j++;
                }
                else $data[$i] .= $c; 
                break;
            default:
                $data[$i] .= $c;
        }
    }
    return $data;
}

$txt = '[12.10.2005 14:34:32] httpd: started http daemon.';

list ($date, $desc, $text) = parseStr($txt);

echo "$date | $desc | $text" ;
?>

//--> 12.10.2005 14:34:32 | httpd | started http daemon.

Link to comment
Share on other sites

Here's the solution using regular expressions. I took it as a challenge to figure this out...

<?php
preg_match_all("/\[(.+)\] (.+).+)$/",$str,$out);
$tmp = array();
foreach ($out as $val)
$tmp[] = $val[0];
list ($dmy,$date1,$desc1,$text1) = $tmp;
echo 'Original string: ' . $dmy . "<br>\n";
echo 'Date: ' . $date1 . "<br>\n";
echo 'Desc: ' . $desc1 . "<br>\n";
echo 'Text: ' . $text1 . "<br>\n";
?>

 

Ken

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.