1 2 3 4 5 6 |
//测试时文件的编码方式要是UTF8 $str='中文a字1符'; echo strlen($str).'<br>';//14 echo mb_strlen($str,'utf8').'<br>';//6 echo mb_strlen($str,'gbk').'<br>';//8 echo mb_strlen($str,'gb2312').'<br>';//10 |
/**
* 只保留字符串首尾字符,隐藏中间用*代替(两个字符时只显示第一个)
* @param string $user_name 姓名
* @return string 格式化后的姓名
*/
function substr_cut($user_name){
$strlen = mb_strlen($user_name, ‘utf-8’);
$firstStr = mb_substr($user_name, 0, 1, ‘utf-8’);
$lastStr = mb_substr($user_name, -1, 1, ‘utf-8’);
return $strlen == 2 ? $firstStr . str_repeat(‘*’, mb_strlen($user_name, ‘utf-8’) – 1) : $firstStr . str_repeat(“*”, $strlen – 2) . $lastStr;
}