php函数substr_replace中文乱码的替代解决方法


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM