ereg_replace出错

时间:2013-08-12 20:23:13

标签: php phpmailer

当我放置“& email&”时在phpmailer的邮件正文中,输出邮件会更改“& email&”到$ to数组中的电子邮件地址。但它只使用第一封电子邮件,它确实看到了其余的。如何让它获取其余的电子邮件并将其相应地放在电子邮件中?

$nq=0;            
for($x=0; $x<$numemails; $x++)
{
    $to = $allemails[$x];
    if ($to)
    {
        $to = ereg_replace(" ", "", $to);

        $message = ereg_replace("&email&", $to, $message);
        $subject = ereg_replace("&email&", $to, $subject);
        $qx=$x+1;

        print "Line $qx . Sending mail to $to.......";

        flush();
    }
}

=== 我无法发布下面的图片链接:

http://filevault.org.uk/testee/mailer_image.png

希望你现在明白。

1 个答案:

答案 0 :(得分:3)

你不应该再使用ereg_*,因为它已被弃用 - preg_replace是它的继承者,尽管看起来你只需要str_replace

$message = str_replace("&email&",$to,$message);

如果由于某种原因你真的必须使用ereg:

您可能需要全局标记g

ereg_replace("&email&g",

每次都有不同的替换

$to = array('email1@me.com','em2@me.com');
$text = 'asdkfjalsdkf &email& and then &email&';
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}
var_dump(preg_replace_callback('#&email&#','replace_emails',$text));
//"asdkfjalsdkf email1@me.com and then em2@me.com" 

集成:

$to = $allemails[$x];
$email_replacements = $to;
function replace_emails()
{
    global $email_replacements;
    return array_shift($email_replacements); //removes the first element of the array of emails, and then returns it as the replacement
}

if($to)
{
    $message = preg_replace_callback('#&email&#','replace_emails',$message);

    $subject = preg_replace_callback('#&email&#','replace_emails',$subject);
    $qx=$x+1;
    print "Line $qx . Sending mail to $to.......";

    flush();