php汇总由逗号分隔的变量,以逗号结尾

时间:2012-12-14 09:35:30

标签: php comma summary

我一直在努力思考这个问题,但我无法给自己一个答案......

例如,当我总结所有发送电子邮件的人时,我想用逗号分隔它们,但最后用一个点!

例如这段代码:

$emailnotificationmessage = "<p id='notification'>An e-mail notification has been sent to: ";
while( $record=mysql_fetch_array($checkwhotosendresult) )
{
    $mailto=$record['email'];
    $mail_ontv = $mailto;

    if (mail($mail_ontv, $onderwerp, $inhoud_mail, $headers))
    {             
        $emailnotificationmessage .= "$mail_ontv, ";
    }
    else
    {
        $emailnotificationmessage = "<p id='notification'>Error. E-mail could not be sent to the receipent.";
    }
}
$emailnotificationmessage .= "</p>";    

它返回:

An e-mail notification has been sent to: mail@mail.com, mail2@mail.com, mail3@mail.com,

那么我怎样才能以一个点结束,以保持语言正确...?

修改

我不想为此发布新问题,只是想知道如何在最后and封电子邮件之间放置2

3 个答案:

答案 0 :(得分:3)

我会以不同的方式解决这个问题(很多方法可以给猫皮肤涂抹)

// create new arrays
$sentemails = $failedemails = array();

while ($record = mysql_fetch_array($checkwhotosendresult))
{
    $mailto = $record['email'];
    $mail_ontv = $mailto;

    if (mail($mail_ontv, $onderwerp, $inhoud_mail, $headers))
    {
        $sentemails[] = $mailto;
    }
    else
    {
        $failedemails[] = $mailto;
    }
}

$emailnotificationmessage = '';

if ( ! empty($sentemails))
{
    $emailnotificationmessage .= '<p>An email notification has been sent to: ' . implode(', ', $sentemails) . '.</p>';
}
if ( ! empty($failedemails))
{
    $emailnotificationmessage .= '<p>Emails could not be sent to: ' . implode(', ', $failedemails) . '.</p>';
}

这将为您提供已发送和失败的电子邮件列表,并且不会使用失败的邮件覆盖已发送的邮件

答案 1 :(得分:2)

使用此

<?php
$emailnotificationmessage="An e-mail notification has been sent to: mail@mail.com, mail2@mail.com, mail3@mail.com,";
$newsentance = substr($emailnotificationmessage, 0,-1).".";    
 echo $newsentance ;
?>

答案 2 :(得分:1)

您可以通过substr

来实现
$emailnotificationmessage = substr($emailnotificationmessage,0,-2) . ".";