请参阅以下代码:
来自:http://www.damnsemicolon.com/php/php-parse-email-body-email-piping
//get rid of any quoted text in the email body
$body_array = explode("\n",$body);
$message = "";
foreach($body_array as $key => $value){
//remove hotmail sig
if($value == "_________________________________________________________________"){
break;
//original message quote
} elseif(preg_match("/^-*(.*)Original Message(.*)-*/i",$value,$matches)){
break;
//check for date wrote string
} elseif(preg_match("/^On(.*)wrote:(.*)/i",$value,$matches)) {
break;
//check for From Name email section
} elseif(preg_match("/^On(.*)$fromName(.*)/i",$value,$matches)) {
break;
//check for To Name email section
} elseif(preg_match("/^On(.*)$toName(.*)/i",$value,$matches)) {
break;
//check for To Email email section
} elseif(preg_match("/^(.*)$toEmail(.*)wrote:(.*)/i",$value,$matches)) {
break;
//check for From Email email section
} elseif(preg_match("/^(.*)$fromEmail(.*)wrote:(.*)/i",$value,$matches)) {
break;
//check for quoted ">" section
} elseif(preg_match("/^>(.*)/i",$value,$matches)){
break;
//check for date wrote string with dashes
} elseif(preg_match("/^---(.*)On(.*)wrote:(.*)/i",$value,$matches)){
break;
//add line to body
} else {
$message .= "$value\n";
}
}
//compare before and after
echo "$body<br><br><br>$message";
$ body包含完整的电子邮件正文,包括引用区域,如果这是一个回复,此循环删除引用区域以获取新回复为$ message。但正如那里所建议的那样,循环很慢而且更好地使用preg_replace。那我怎么办?
用什么代替模式?我应该删除foreach循环吗?我创建下面没有foreach循环,但似乎错了?请建议。
$patterns = array(
"_________________________________________________________________",
"/^-*(.*)Original Message(.*)-*/i",
"/^On(.*)wrote:(.*)/i",
"/^On(.*)$fromName(.*)/i",
"/^On(.*)$toName(.*)/i",
"/^(.*)$toEmail(.*)wrote:(.*)/i",
"/^(.*)$fromEmail(.*)wrote:(.*)/i",
"/^>(.*)/i",
"/^---(.*)On(.*)wrote:(.*)/i");
$message = preg_replace($patterns, '', $body);
答案 0 :(得分:1)
您已经将其缩小为可行的解决方案。只有几件事要解决:
/m
设置^
修饰符,以便在每行的开始时匹配。^
和行尾,以保持与原始代码中相同的含义。$fromName
,$fromEmail
等。(?s:.*)
匹配从那里到身体末端的所有内容。$patterns = array(
"/^_{30,}$(?s:.*)/m",
"/^.*Original Message(?s:.*)/im",
"/^(?:---.*)?On .* wrote:(?s:.*)/im",
"/^On .* $fromName(?s:.*)/im",
"/^On .* $toName(?s:.*)/im",
"/^.*$toEmail(.*)wrote:(?s:.*)/im",
"/^.*$fromEmail.* wrote:(?s:.*)/im",
"/^>.*/ims",
);
$message = preg_replace($patterns, '', $body);
echo "$body<br><br><br>$message";
考虑到它也会删除如下行:
only thing I wrote: ...