【发布时间】:2013-01-17 13:13:51
【问题描述】:
我在执行命令 mailq 并将其输出转换为多维数组中的值时遇到问题。
$results = array();
exec('/usr/bin/mailq', $mailQresult);
foreach ($mailQresult as $key => $mailqLine) {
$res = preg_match('/^([A-Z0-9]{1,20})\s+([1-9][0-9]*)\s+((Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+[A-Za-z]{3}\s+[0-9]{2}\s+[0-9]{2}\:[0-9]{2}\:[0-9]{2})\s*$/', $mailqLine, $matches);
if ($res) {
// Insert message ID and other data into the object.
// It will be competed afterwards.
$mailqArray = array(
'id' => $matches[1],
'size' => intval($matches[2]),
'date' => strftime($matches[3]),
'sender' => $matches[5],
'failed' => false,
'recipients' => $matches[4]
);
}
}
mailq 的输出包含空格并返回,看起来有点像这样:
ID SIZE DAYNAME MONTH DAY HOUR:MINUTES:SECONDS sender@address.com [new line here]
(Host or domain name not found. Name service error for name=address.com type=MX: Host not found, try again) [new line here]
recipient@address.com
所以错误消息和接收者最终使用不同的键。
我知道我可以使用 shell_exec 将 mailq 的输出作为一个字符串获取,但我不知道如何编写将创建多维数组的正则表达式。
我可能接近这个错误。非常欢迎任何建议!
【问题讨论】:
标签: php regex linux arrays postfix-mta