$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函数截取函数再连接字符串解决,虽然有点丑陋,但能解决问题。

相关文章:

  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2022-01-12
  • 2021-09-06
  • 2022-12-23
  • 2022-01-19
  • 2022-02-12
猜你喜欢
  • 2022-12-23
  • 2021-12-05
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
相关资源
相似解决方案