I have the following log:
#VERSION: 2.9 2017-02-24 10:03:39 channel_ping 1afcd2e2857045fe973e7ed036ea450c dfe17897d80b44f6901593364fc72b55 2479884 127.0.0.1 Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_11_6)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/56.0.2924.87%20Safari/537.36 https://test.com/ TEST-User-50187 42134f99b57240d19e05f760800a9dc6
I can read the content of this in PHP no problem which outputs:
#VERSION: 2.9 2017-02-24\t10:03:39\tchannel_ping\t1afcd2e2857045fe973e7ed036ea450c\tdfe17897d80b44f6901593364fc72b55\t2479884\t127.0.0.1\tMozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_11_6)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/56.0.2924.87%20Safari/537.36\thttps://test.com/\tTEST-User-50187\t42134f99b57240d19e05f760800a9dc6 """
I need to be able structure this into something like so:
array[0] = '2017-02-24'; array[1] = '10:03:39'; array[2] = 'channel_ping';
You can explode your output:
$arr = explode("\\t",$linesOf);
Or if $linesOf already includes the \t as a normal string:
$arr = explode("\t",$linesOf);
Before I would make a substring to get rid of the "#VERSION: 2.9 " part...
You can use a regex with preg_match_all:
preg_match_all("/
(\w+?-\w+?-\w+)\t(\w+?:\w+?:\w+)\t([\w_]+)/", $strLog, $matches, PREG_PATTERN_ORDER);
That return an array:
Array
(
[0] => Array
(
[0] =>
2017-02-24 10:03:39 channel_ping
)
[1] => Array
(
[0] => 2017-02-24
)
[2] => Array
(
[0] => 10:03:39
)
[3] => Array
(
[0] => channel_ping
)
)
Finally, get values:
$date = $matches[1][0];
$time = $matches[2][0];
$command = $matches[3][0];
You can use the following.
use preg_split, with splitting on all 'space' characters \s
$res = preg_split('/\s/i',$str);
Then, because there will be "empty strings" that is useless for us, we filter with array_filter the results to remove those:
$res = array_filter($res, function($elem) {
// if it's not empty after trimming we can use it.
if (!empty(trim($elem))){
return true;
}
return false;
});
See it in acion:
$str = "#VERSION: 2.9
2017-02-24\t10:03:39\tchannel_ping\t1afcd2e2857".
"045fe973e7ed036ea450c\tdfe17897d80b44f6901593364fc72b55\t2479884\t127.0.0.1\tM".
"ozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_11_6)%20AppleWebKit/537.3".
"6%20(KHTML,%20like%20Gecko)%20Chrome/56.0.2924.87%20Safari/537.36\thttps://test".
".com/\tTEST-User-50187\t42134f99b57240d19e05f760800a9dc6
";
$res = preg_split('/\s/i',$str);
$res = array_filter($res, function($elem) {if(!empty(trim($elem))){return true;}return false; });
var_dump($res);