widowmaker Posted July 21, 2010 Share Posted July 21, 2010 hiya, I'm having this problem. I've got a log file which looks similar to this: 2010/06/28 22:54:59 administrator 13 -1 2010/06/06 22:54:59 administrator 1 2 2010/06/06 22:55:04 user 1 1 I want to go thru the whole logfile and split each row into variables using this pattern: $reg[0]=2010 $reg[1]=06 $reg[2]=28 $reg[3]=22 $reg[4]=54 $reg[5]=59 $reg[6]=administrator $reg[7]=13 $reg[8]=-1 I'm using this code but it doesn't work for some reason: $row = '2010/06/28 22:54:59 administrator 13 -1'; preg_match_all('/^(\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\d{2})\d{2})\s(\w+?)\t(\d+?)\t(\d+)$/i', $row, $regs); var_dump($regs); thanx for any help Quote Link to comment Share on other sites More sharing options...
widowmaker Posted July 21, 2010 Author Share Posted July 21, 2010 I get an empty array instead of filled: array(10) { 0 => array(0) 1 => array(0) 2 => array(0) 3 => array(0) 4 => array(0) 5 => array(0) 6 => array(0) 7 => array(0) 8 => array(0) 9 => array(0) } any help? thanx Quote Link to comment Share on other sites More sharing options...
.josh Posted July 22, 2010 Share Posted July 22, 2010 first thing i notice is looks like you aren't checking for negative numbers. Quote Link to comment Share on other sites More sharing options...
salathe Posted July 22, 2010 Share Posted July 22, 2010 This would be much easier without regular expressions, there are tools in PHP dedicated to parsing this kind of (tab-separated value) file, or more useful (and simple) string-parsing functions to do the job manually. Do you ultimately want the array to be like that you posted (in the first post) or would you like the date and time values kept together? Quote Link to comment Share on other sites More sharing options...
widowmaker Posted July 22, 2010 Author Share Posted July 22, 2010 ok I don't mind, the date and time could be together, the most important for me is this part: $reg[6]=administrator $reg[7]=13 $reg[8]=-1 including the fact that $reg[8] can be non-positive Quote Link to comment Share on other sites More sharing options...
salathe Posted July 22, 2010 Share Posted July 22, 2010 One way would be to use sscanf like: $row = '2010/06/28 22:54:59 administrator 13 -1'; $reg = sscanf($row, '%d/%d/%d %d:%d:%d %s %d %d'); And another would be to use fgetcsv and specify tab as the delimiter character. (The function is named CSV for comma-separated values, but is equally happy with tab-separated values like your file). Quote Link to comment 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.