creiss Posted December 26, 2007 Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/ Share on other sites More sharing options...
drummer101 Posted December 26, 2007 Share Posted December 26, 2007 You'll have to use 3 different vars for it, but explode() is what you're looking for http://us2.php.net/explode Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423758 Share on other sites More sharing options...
creiss Posted December 26, 2007 Author Share Posted December 26, 2007 You'll have to use 3 different vars for it, but explode() is what you're looking for http://us2.php.net/explode I'd agree, but so far it seems that I can'T specify start and ending points with explode, only a dividing delimiter and a limit. But the limit is not known, as it is another, different char. -Chris. Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423763 Share on other sites More sharing options...
AndyB Posted December 26, 2007 Share Posted December 26, 2007 split on ] to produce two array variables (strings) str_replace the [ from the first, and explode the second on the : delimiter to produce the other two parts Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423767 Share on other sites More sharing options...
kenrbnsn Posted December 26, 2007 Share Posted December 26, 2007 This seems like a task for regular expressions, but since I'm no good at them, I'll leave it up to someone who does them to answer... Ken Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423769 Share on other sites More sharing options...
drummer101 Posted December 26, 2007 Share Posted December 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423770 Share on other sites More sharing options...
creiss Posted December 26, 2007 Author Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423771 Share on other sites More sharing options...
Northern Flame Posted December 26, 2007 Share Posted December 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423775 Share on other sites More sharing options...
kenrbnsn Posted December 26, 2007 Share Posted December 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423777 Share on other sites More sharing options...
drummer101 Posted December 26, 2007 Share Posted December 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423778 Share on other sites More sharing options...
creiss Posted December 26, 2007 Author Share Posted December 26, 2007 You guys are awesome. Solved my problem in under half an hour. I will test the solutions tomorrow, as its 01:00 am here, and time for bed. Cya all tomorrow, and thanks again! -Chris. Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423781 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423783 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2007 Share Posted December 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/83288-breaking-down-a-string/#findComment-423799 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.