<?php
$str = file_get_contents('/Users/chenbingbing/Downloads/product-slow-report.txt');
$str = preg_replace('##s+(?!Querys*d+)[^
]+#', '', $str);
$str = preg_replace("#(
)+#", "
", $str);
$sqlKeys = array('SELECT', 'SET', 'SHOW', 'administrator');
$sqlKeys = implode('|', $sqlKeys);
preg_match_all('#ID (.*) at byte.*
((' . $sqlKeys . ').*)\G#iUs', $str, $matches);
$sep = " ";
foreach ($matches[1] as $k => $id) {
$sql = $matches[2][$k];
$sql = str_replace('G', '', $sql);
preg_match('#/* (.*)*/#', $sql, $match);
if (!empty($match)) {
$sql = trim(str_replace($match[0], '', $sql));
$visit = trim($match[1]);
} else {
$visit = '';
}
$sql = preg_replace('#s+#', ' ', $sql);
echo $id . $sep . '"' . $visit . '"' . $sep . '暂无' . $sep . '"' . $sql . '"';
echo PHP_EOL;
}
//