我正在创建一个脚本,可以找到我服务器上的所有error_log文件。以下是典型error_log文件的示例:
[08-Feb-2012 05:45:56] PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php:308
Stack trace:
#0 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(238): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('')
#1 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(96): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(60): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
[08-Feb-2012 05:46:18] PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php:308
Stack trace:
#0 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(238): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('')
#1 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(96): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 /lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php(60): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
[15-Feb-2012 15:39:10] PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in /settings.php on line 47
我使用以下代码单独提取每个错误,以便我可以显示它们以便于查看
preg_match_all('/\[\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2}\].*/', $error_log_file, $error_log_parts);
它有效,但问题是没有选择多行。
Array
(
[0] => Array
(
[0] => [08-Feb-2012 05:45:56] PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /home/elvanto/public_html/lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php:308
[1] => [08-Feb-2012 05:46:18] PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /home/elvanto/public_html/lib/SwiftMailer/classes/Swift/Mime/Headers/MailboxHeader.php:308
[2] => [15-Feb-2012 15:39:10] PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in /home/elvanto/public_html/roster-settings.php on line 47
)
)
所以我在最后加了一个's':
preg_match_all('/\[\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2}\].*/s', $error_log_file, $error_log_parts);
但现在似乎选择了一切。我需要一种方法来选择一切,直到新的约会。例如:[08-Feb-2012 05:46:18]
我需要添加哪些代码才能执行此操作?
答案 0 :(得分:1)
尝试类似
的内容/\[\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2}\].*?(?=^\[)/s
.*
已变为非贪婪.*?
,并添加了(?=^\[)
以强制.*?
匹配,直到找到以{开头的换行符{1}}。
顺便说一句,您可以连续读取日志文件的行,如果看到[
并且连接到当前数组条目则开始新的数组条目 - 否则您不必放屁使用\[\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2}\]
/ .*
切换。