快乐每一天

自己选择的路,跪着也要走下去...

PHP 截取中文中某一部分为*号显示

public function substr_cut2($user_name){
        $strlen     = mb_strlen($user_name, 'utf-8');
        if($strlen<2){
            return $user_name;
        }else{
            $firstStr     = mb_substr($user_name, 0, 1, 'utf-8');
            $lastStr     = mb_substr($user_name, -1, 1, 'utf-8');
            if($strlen>4){
                $firstStr     = mb_substr($user_name, 0,1, 'utf-8');    

                // iconv_set_encoding("internal_encoding", "utf-8");

                if (PHP_VERSION_ID < 50600) {  
                    iconv_set_encoding('input_encoding', 'UTF-8');  
                    iconv_set_encoding('output_encoding', 'UTF-8');  
                    iconv_set_encoding('internal_encoding', 'UTF-8');  
                } else {  
                    ini_set('default_charset', 'UTF-8');  
                }


                $lastStr     = iconv_substr($user_name,-($strlen-3));
                 // $lastStr     = mb_substr($user_name, -1,$strlen-3, 'utf-8');
                 return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*",2) . $lastStr;


            }else{
                return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", 2) . $lastStr;
            }
        }
    }
none
此文章共有条评论, 人参与 |Powerd By Angboo