I'm attempting to parse through a log file containing numerous traces, some of which have multiple lines to them.
Example:
[trace-123] <request>This is a log line</request>
[trace-124] <reply>This is another log line
this is part of "[trace-124]" still.</reply>
[trace-125] <request>final log line.</request>
I'm attempting to use preg_match_all to get an array of all the traces.
$file = file_get_contents("traces.txt");
$tracePattern = "/(\[trace-[0-9]*+\]+[\s\S]*)(?<=\<\/reply>|\<\/request>)/";
preg_match_all($tracePattern,$file,$lines);
echo "<pre>";print_r($lines);echo "</pre>";
Ideally, I'd like my results to look like this:
Array
(
[0] => [trace-123] <request>This is a log line</request>
[1] => [trace-124] <reply>This is another log line
this is part of "[trace-124]" still.</reply>
[2] => [trace-125] <request>final log line.</request>
)
but when I run it, I get an array with everything in 1 element of the array. When I wrote the expression, my goal was to basically look for:
[trace-\[0-9]*\]
and find everything from that match to the next match of it.
I found that
\[trace-[0-9]*+\].*
works pretty well, but breaks down when there are line breaks.
[trace-at the beginning of the string. Each time you encounter this value, start adding the lines to a string at the next array position in teh array you are building. You stop adding to this string the next time you encounter either another line beginning with[trace-or the next time you encounter a line beginning with some other non-trace signature (like for instance if you have lines like[error-or whatever).[trace-is seen, which basically requires the implementation I suggested above to be to execute). This may not be feasible for larger log files. You likely should focus on a solution that allows you to parse and work with a single line at a time.