I have different servers
$strarrServerIp = array('192.168.123.1','192.168.123.2');
$strarrLogsData = array();
foreach($strarrServerIp as $strServerIp ){
$strCommand = 'ssh abose@' . $strServerIp . ' cat /srv/www/vhosts/trunk/Logs/Log20130825.psi.log';
$strarrLogsData[] = shell_exec( $strCommand );
}
foreach( $strarrLogsData as $strJsonLogData ){
foreach( preg_split("/((\r?\n)|(\r\n?))/", $strJsonLogData ) as $strJsonLineLog ) {
display($strJsonLineLog);
}
}
The servers contain files containing JSON logs in this form.
{"timestamp":"2013-08-25 20:35:06 MDT","severity":"INFO","data":"1"}
{"timestamp":"2013-08-25 20:36:06 MDT","severity":"INFO","data":"15"}
{"timestamp":"2013-08-25 20:37:06 MDT","severity":"INFO","data":"32"}
{"timestamp":"2013-08-25 20:38:06 MDT","severity":"INFO","data":"25"}
{"timestamp":"2013-08-25 20:39:06 MDT","severity":"INFO","data":"5"}
I want to separate them into an array
array (
[0] => {"timestamp":"2013-08-25 20:35:06 MDT","severity":"INFO","data":"1"}
[1] => {"timestamp":"2013-08-25 20:36:06 MDT","severity":"INFO","data":"15"}
[2] => {"timestamp":"2013-08-25 20:37:06 MDT","severity":"INFO","data":"32"}
[3] => {"timestamp":"2013-08-25 20:38:06 MDT","severity":"INFO","data":"25"}
[4] => {"timestamp":"2013-08-25 20:39:06 MDT","severity":"INFO","data":"5"}
)
Any suggestions? I want something better than
preg_split("/((\r?\n)|(\r\n?))/", $strJsonLogData )
Try this, you can also use file() function to get each line separated in an array.
$text = '
{"timestamp":"2013-08-25 20:35:06 MDT","severity":"INFO","data":"1"} {"timestamp":"2013-08-25 20:36:06 MDT","severity":"INFO","data":"15"}
{"timestamp":"2013-08-25 20:37:06 MDT","severity":"INFO","data":"32"}
{"timestamp":"2013-08-25 20:38:06 MDT","severity":"INFO","data":"25"}
{"timestamp":"2013-08-25 20:39:06 MDT","severity":"INFO","data":"5"}';
$array = preg_split("#}.*?{#s", $text );
$array = array_map("trim", $array);
print_r($array);
//using preg_match_all
preg_match_all("#({.*?})#s", $text, $matches);
print_r($matches[1]);
用\\n
分割并在每行上使用json_decode
。
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.