Jump to content

preg_match_all


widowmaker

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

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.