我试图在一个循环中连接一个字符串,当它在字符串上时是空白的(或者之前的任何值)。我不确定发生了什么,但我似乎无法找到类似的问题。
$toItem = "";
$recpItem = "";
if(is_array($to))
{
foreach($to as $key=>$email)
{
$recpItem = $recpItem.'<item><recip dn="'.$email.'" sa="'.$email.'" ra="'.$email.'" al="" rt="SMTP" id="" ao="3" rf="0" ei="0" uri=""/></item>';
$email = htmlentities($email);
$toItem = $toItem.'<item><Rcp DN="'.$email.'" EM="'.$email.'" RT="SMTP" AO="3"/></item>';
}
}
答案 0 :(得分:0)
我会将代码添加到数组中,然后implode()
:
$to = array(
'bob@gmail.com',
'sally@gmail.com'
);
if(is_array($to)){
$recpItem = array();
$toItem = array();
foreach($to as $email){
$recpItem[] = '<item><recip dn="'.$email.'" sa="'.$email.'" ra="'.$email.'" al="" rt="SMTP" id="" ao="3" rf="0" ei="0" uri=""/></item>';
$email = htmlentities($email);
$toItem[] = '<item><Rcp DN="'.$email.'" EM="'.$email.'" RT="SMTP" AO="3"/></item>';
}
$recpHtml = implode($recpItem);
$toHtml = implode($toItem);
echo $recpHtml;
echo '<hr />';
echo $toHtml;
}