$str = "*";
$len = mb_strlen($str,'utf-8');
if($len>=6){
$str1 = mb_substr($str,0,2,'utf-8');
$str2 = mb_substr($str,$len-2,2,'utf-8');
}
else{
$str1 = mb_substr($str,0,1,'utf-8');
$str2 = mb_substr($str,$len-1,1,'utf-8');
} $this->assign('str1',$str1);
$this->assign('str2',$str2);
$this->assign('str',$str1.'**'.$str2);
$this->assign('strold',substr_replace($str,'**',2,2));
输出结果为:
中华
和国
中华**和国
��**��人民*
substr_replace函数有些缺陷,中文替代会出现乱码,使用mb_substr函数截取函数再连接字符串解决,虽然有点丑陋,但能解决问题。