utf-8编码时统计字数

写该程序的目的是因为utf-8编码时,中文和数字、英文字母所占字节数不同,在进行截取时不方便处理。

[php]
function utf8_array(msg){result=array();
strlen = strlen(msg);
for(i=0;i<strlen;i++) {
str=substr(msg, i, 1); if(ord(str)>0xA0 ){
result[]=substr(msg, i, 3);i =i+2; }else{result[]=substr(msg,i,1);
}
}
return result; } //下面为示例 function sendSms(tele_num,msg){ header(“Content-type:text/html;charset=utf-8”);sms=D("sms");
msg_data=utf8_array(msg);
msg_count=count(msg_data);//字数
msg_num=1;//短信条数 if(msg_count>65){
msg_num=ceil(msg_count/62);
}

if(msg_num==1){insert_data=array("is_sent"=>0,"tele_num"=>tele_num,”add_time”=>time(),”send_time”=>0,”send_content”=>msg);
sms->add(insert_data);
}else{
for(i=0;i<msg_num;i++){
msg_send=””; for(j=i*62;j<(i+1)*62;j++){
if(j<msg_count)
msg_send.=msg_data[j]; }msg_send.="(".(i+1).”)”;insert_data=array("is_sent"=>0,"tele_num"=>tele_num,”add_time”=>time(),”send_time”=>0,”send_content”=>msg_send);
sms->add(insert_data);
}
}

}
[/php]

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据