preg_matchの代わりにpreg_replaceを使用してこのコードを短縮することは可能ですか?
メール本文から引用テキストを削除するために使用しています。引用されたテキストは、電子メールに返信するときに誰かを引用するときのものです。
# Get rid of any quoted text in the email body
# stripSignature removes signatures from the email
# $body is the body of an email (All headers removed)
$body_array = explode("\n", $this->stripSignature($body));
$new_body = "";
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 {
$new_body .= "$value\n";
}
}
これはほぼ機能しますが、最初の行は「2012年7月30日月曜日の午後10時54分、PersonsNameは次のように書いています」を保持しています。
$body = preg_replace('/(^\w.+:\n)?(^>.*(\n|$))+/mi', "", $body);