Reputation: 21
I have a huge text file that contains some data that I want to insert in my database. The problem is that I don't want to insert every data, and I can't seem to make any preg_match_all(), in fact I'm a little newbie with regular expressions:
file.txt
[03-07-2010 09:03:23] : [180.20.106.107] : [/success]:
[SEND] invite -> helen,
[SEND] uname -> test,
[SEND] fname -> test,
[03-07-2010 09:04:28] : [180.20.106.107] : [/success]:
[SEND] invite -> helen3,
[SEND] uname -> test3,
[SEND] fname -> test3,
[SEND] register -> register,
[03-07-2010 09:07:43] : [180.20.106.107] : [/success]:
[SEND] invite -> register,
[SEND] uname -> helen2,
[SEND] fname -> none,
[03-07-2010 09:09:48] : [180.20.106.107] : [/success]:
[SEND] invite -> helen2,
[SEND] uname -> test2,
[SEND] fname -> test2,
[03-07-2010 10:14:18] : [180.20.106.107] : [/success]:
[SEND] invite -> register,
[SEND] uname -> mickey,
[SEND] fname -> test8,
[SEND] register -> register,
I want to extract only the block that has register
, that means from the start [03-07-2010 09:04:28]
till the end register -> register,
and [03-07-2010 10:14:18]
to the end of that block (register -> register
).
I have tried a lot of things, but like I said, I'm a newbie in regex
This does not work:
#\[(.*)\] : \[(.*)\] : \[\/success\]:(.*)register -> register#s
Upvotes: 2
Views: 87
Reputation: 324640
There's a few places where you should use ungreedy quantifiers, but that's not the problem.
The issue is that you are trying to get data on another line. .
does not match newlines unless you specifically tell it to with the s
modifier.
That being said, you can't do this with a single regexp, because it will match the first line that has the timestamp and IP, then skip all the way to the register -> register
line and say "oh hey it works!"
Instead, I suggest splitting your subject string into indiviudal blocks, like so:
$blocks = preg_split("/\r?\n\s*\r?\n/",$inputText);
foreach($blocks as $block) {
list($first,$data) = explode("\n",$block,2);
if( preg_match("/\bregister\s*->\s*register\b/",$data)) {
preg_match_all("/\[(.*?)\]/",$first,$m,PREG_SET_ORDER);
list($timestamp,$ip) = $m[0];
// do something here with the information.
}
}
Upvotes: 2