我的日志文件显示为
text message sent to client 1234 and mobileNo: 987654
Message: This is working
Confirmation No: ab123
text message sent to client 4321 and mobileNo: 456789
Message: This is not working
Confirmation No:
如果没有确认号码(即确认号码),我必须收到提醒
我写了以下代码:
preg_match('/Confirmation No:\s*$/', $logLine, $match);
$matchLine = implode(" ",$match);
if ($matchLine == NULL) {
echo "There is an empty Confirmation No";
} else {
echo "Confirmation No: " . $matchLine;
}
这样可以正常工作,如果没有确认,则打印“有空确认号”。
但我想在输出中添加clientID和MobileNo,即 MobileNo上的客户端4321有一个空的确认:456789。
有人可以建议,我该怎么做?
提前致谢
答案 0 :(得分:0)
preg_match("/Confirmation\sNo:\s(.*)..*client\s(\d+)\sand\smobileNo:\s(\d+)/ms", $string, $match);
if ($match[1] == "") {
echo "There is an empty Confirmation No for client " . $match[2] . " on MobileNo: " . $match[3];
} else {
echo "Confirmation No: " . $match[1];
}
https://regex101.com/r/qU7aP2/1
编辑:我刚注意到,也许是另一种方式? https://regex101.com/r/oY4hD9/1$pattern = "/client\s(\d+)\sand\smobileNo:\s(\d+)..*Confirmation\sNo:\s(\w+)/ms";
答案 1 :(得分:0)
以下是我的工作方式:
// within the loop on the log file
if (preg_match('/\b(client \d*) and \b(mobileNo: \d*)/', $logLine, $m)) {
$client = 'There is an empty confirmation for '.$m[1].' on '.$m[2];
}
if (preg_match('/Confirmation No: (\w*)$/', $logLine, $m) {
if (empty($m[1])) {
echo $client;
} else {
echo $logLine;
}
$client = '';
}