Reputation: 75
I have log file which looks like below:
--f15a0000-A--
[30/Aug/2018:14:06:33 +0200] W4fdyYHC0Xb8YDuIqk5YQgAAAD0 127.0.0.1 55454 127.0.0.1 80
--f15a0000-B--
GET /FormValidation/page1.php HTTP/1.1
Host: localhost
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost/FormValidation/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343
--f15a0000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 851
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
--f15a0000-Z--
--bb410000-A--
[30/Aug/2018:14:06:37 +0200] W4fdzYHC0Xb8YDuIqk5YQwAAAD0 127.0.0.1 55454 127.0.0.1 80
--bb410000-B--
POST /FormValidation/validation.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 33
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/FormValidation/page1.php
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
tanuser: 00198343
--bb410000-C--
name1=test&email1=ssn%40gmail.com
--bb410000-F--
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.35
Content-Length: 17
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
--bb410000-Z--
I want to build an array containing one block per position.
Ex-ex:
array[0][‘rawdata’]
contains the first block from --f15a0000-A-- to --f15a0000-Z--
,
array[1][‘rawdata’]
contains the second block from --bb410000-A-- to --bb410000-Z--
.
Can anyone please help me how to do that?
Upvotes: 0
Views: 271
Reputation: 677
Read through the file and every time you find a -- in the beginning you either start or stop an array-entry.
$array = array();
$fh = fopen($file,"r");
$started = false;
while (!feof($fh)) {
$line = fgets($fh);
if($started) {
$temp .= $line;
if(strpos($line, "--") === 0 && strpos($line, "-Z-") > 0) {
$started = false;
$array[] = $temp;
}
}
if(strpos($line, "--") === 0 && strpos($line, "-A-") > 0) {
$started = true;
$temp = $line;
}
}
fclose($fh);
print_r($array);
I think this should do it
Upvotes: 2
Reputation: 5224
It seems like there is a pattern here so you could use a regex and preg_match_all
. A regex such as
--([a-z\d]+)-A--[\s\S]+?--\1-Z--
seems to work with your sample data.
The --
is literal.
[a-z\d]
is a character a
through z
and \d
is a number.
The +
allows it to be one or more of those characters.
The ()
captures the group.
The -A--
is a literal search (the A
seems to be a starting block.)
The \s\S
is a white space or non-whitespace aka just keep searching
The +?
is to find a match until the next string is found.
The next string we search is the same identifier we started with but with a Z for the closing block, --\1-Z--
. The \1
is a back reference to the first capture group we created.
https://regex101.com/r/Gu6Qma/1/
PHP usage: https://3v4l.org/rjtW7
preg_match_all('/--([a-z\d]+)-A--[\s\S]+?--\1-Z--/', $string, $matches);
print_r($matches);
Note the PHP usage requires starting and ending delimiters on the regex as well.
Upvotes: 5