当前位置:Gxlcms > PHP教程 > php去除数组内空格元素的有关问题

php去除数组内空格元素的有关问题

时间:2021-07-01 10:21:17 帮助过:13人阅读

php去除数组内空格元素的问题
各位大虾级程序员:
俺是刚入门的PHP菜鸟.现在请教一个问题:
因公司需要,我下载并安装了一个Roundcube Wemail,按公司的要求自己在里面写一些必要的插件程序。其中在写转发邮件地址时,我是把要转发的地址写在一个变量中,然后利用explode()函数以回车符号分隔成各个数组元素,但发现只要保存后,最后一个元素总为空,怎以也删除不掉这个元素,这个问题困扰了我三天了。特来请教大家,以下是相关的程序:
从数据库中获取的字段
$user_saved_forward_email=forwardemail_qry(); //创建记录集
$field_id='forward_email_addresss';

if ($user_saved_forward_email!=null)
{
$myemail='';
$email_arr= explode(";",$user_saved_forward_email[1]);//如果允许转发,则以";"从数据表该字段中分隔各元素并显示出来

foreach($email_arr as $k=>$v)
{
if($k=count($email_arr-1))
{
$myemail = $myeamil.$v;
}else{
$myemail=$myemail.$v.chr(10);
}
}

html显示前台格式:
$input_textarea = new html_textarea(array('name' => 'forward_email_address', 'id' => $field_id,
'rows'=>24,'cols'=>80,'wrap'=>true,'value'=>$myemail));

把前台输入的字符串转化为数组,再用chr(10)分隔开数组内每一个元素,最后以“;”分隔写进数据库
$forward_email_address = get_input_value('forward_email_address', RCUBE_INPUT_POST);
// 把转发的所有地址转化为数组
$forward_email_address_Array = explode(chr(10),$forward_email_address);
//统计数组元素个数 $aaa;
$forward_address =array_unique($forward_email_address_Array); //获取不重复的数组元素
if(count($forward_address)!=count($forward_email_address_Array)) //如果有重复的邮件
{
$forward_count =count($forward_address);
$rcmail->output->command('display_message',$this->gettext('forward_repeat'),'confirmation');
return FORWARDEMAIL_ERROR;
}
$email_count =count($forward_address);
$forward_email_address=null; //赋初值为NULL

for($i=0;$i<=($email_count-1);$i++) //遍历所有的数组元素
{
if(strlen(trim($forward_address[$i]))>2) //如果元素不为空
{
if(ereg("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",trim($forward_address[$i]))) //如果符合电邮格式
{

if($forward_address[$i]==$_SESSION['username']) //如果转发的是自身,则返回错误信息
{
$rcmail->output->command('display_message',$this->gettext('forward_self'),'confirmation');
return FORWARDEMAIL_ERROR;

}else //赋值给$forward_email_address变量后再加上";"
{
$forward_email_address =trim($forward_email_address.$forward_address[$i]).";";

}
}
else //如果元素不符合格式将返回错误信息!
{
$forward_email_enabled="N";
$rcmail->output->command('display_message',$this->gettext('forward_incorrect'),'confirmation');
echo "That Email Format is Wrong!";
return FORWARDEMAIL_ERROR;

人气教程排行