how would i go about splitting the following format string into an array?
[13:05:37] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] hi[m
the string format is from a mine craft server and i believe the format is
[time] [thread info]: ANSICOLOROPEN[stringANSICOLORCLOSE] string EOL
how could i split this so the array would be
[0]time
[1]thread info
[3]ansi open
[4]string
[5]ansi close
[6]string
[7]EOL marker
I tend to start by exploding these types of log entries on the spaces and then hard-code in the index values since those tend not to change. For example:
$log_line = "[13:05:37] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] hi[m";
$line_parts = explode(" ",$log_line);
$log_data = array(
'time' => $line_parts[0],
'thread_info' => $line_parts[1],
'somethingansirelated' => $line_parts[2]
);
It's much easier to further clean-up your various log datas from here in a variety of ways, for example you could use array_map:
array_map( function( $a ){
// trim, format, validate, etc.
}, $log_data );
Not sure if this will get mauled as a non-answer or not but it might help, so here you go.
You can try to get all items enclosed by brackets using regular expressions, eg:
$re = "/\[([^\]]+)\]/";
$str = "[13:05:37] [Server thread/INFO]: [0;35;1m[Server[0;35;1m] hi[m]";
preg_match_all($re, $str, $matches);
Then you can iterate thru the $matches
variable which is an array of the possible matches of the pattern being used against your string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.