0

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
SSneha
  • 75
  • 10
  • 2
    What have you tried? Show us some code. – Rikudou_Sennin Aug 30 '18 at 12:12
  • 1
    Open the file, read it, when you see the tag you want, store it in your array. Or read the entire file once, into an array, grep out what you do not need. Try something, we can *help*. – Nic3500 Aug 30 '18 at 12:14
  • Actually i have no idea how to start. I don't want any code. it will be really helpful if you can just explain what can i do. Thanks much – SSneha Aug 30 '18 at 12:14
  • Possible duplicate of [how to create a logfile in php?](https://stackoverflow.com/questions/19898688/how-to-create-a-logfile-in-php) – lazzy_ms Aug 30 '18 at 12:26
  • Open, read, ... functions: http://php.net/manual/en/ref.filesystem.php or read entire file: `file_get_contents` or `file`. – Nic3500 Aug 30 '18 at 12:27

2 Answers2

5

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.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • A possible additional worth note is you want the `0` index of `$matches`. The `1` index is the first capture group, which isn't important after the search. `print_r($matches[0]);` – user3783243 Aug 30 '18 at 12:52
  • Terrific answer. Please always post complete, thoughtful, educational answers like this. We need more posters like you! Good on ya. – mickmackusa Aug 30 '18 at 13:36
2

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

Dieter Kräutl
  • 657
  • 4
  • 8