设置字符集后怎么还乱码
$load_list 是一个二维数组,将其中的user_name值取出前两个字符,后面用星号代替,但是设置字符集后还是有乱码,但是中文是要显示前两个字符,后面用星号表示
foreach($load_list as $k=>&$v){
$temp=&$v['user_name'];
//判断是否有中文
if (!preg_match('/^([\x81-\xfe][\x40-\xfe]){0,2}$/',$temp)){
$len=mb_strlen($temp,'utf-8');
}else{
$len=strlen($temp);
}
for($k=0;$k<$len;$k++){
if($k>1){
$temp[$k]='*';
}
设置字符集后还乱码
[解决办法]
$temp = '判断是否有中文';判断*****
//判断是否有中文
if(preg_match('/[\x81-\xfe][\x40-\xfe]/',$temp)){
mb_internal_encoding("gbk");
}else {
mb_internal_encoding("utf-8");
}
$len = mb_strlen($temp);
$t = '';
for($k=0; $k<$len; $k++){
if($k>1) {
$t .= '*';
}else {
$t .= mb_substr($temp, $k, 1);
}
}
echo $t;
$load_list =array(
array('user_name' => 'xiaodg'),
array('user_name' => 'x42466'),
array('user_name' => 'etea'),
array('user_name' => 'na0617'),
array('user_name' => 'kimo'),
array('user_name' => '留影'),
array('user_name' => 'vian22'),
array('user_name' => 'qingyu'),
array('user_name' => 'jiangliman'),
array('user_name' => 'xiaobu'),
array('user_name' => 'xiaobu'),
array('user_name' => 'hfa818'),
);
foreach($load_list as $k=>&$v){
$temp=$v['user_name'];
//判断是否有中文
if(preg_match('/[\x81-\xfe][\x40-\xfe]/',$temp)){
mb_internal_encoding("gbk");
}else {
mb_internal_encoding("utf-8");
}
$len = mb_strlen($temp);
$t = '';
for($k=0; $k<$len; $k++){
if($k>2) {
$t .= '*';
}else {
$t .= mb_substr($temp, $k, 1);
}
}
$v['user_name'] = $t;
}
print_r($load_list);
Array
(
[0] => Array
(
[user_name] => xia***
)
[1] => Array
(
[user_name] => x42***
)
[2] => Array
(
[user_name] => ete*
)
[3] => Array
(
[user_name] => na0***
)
[4] => Array
(
[user_name] => kim*
)
[5] => Array
(
[user_name] => 留影
)
[6] => Array
(
[user_name] => via***
)
[7] => Array
(
[user_name] => qin***
)
[8] => Array
(
[user_name] => jia*******
)
[9] => Array
(
[user_name] => xia***
)
[10] => Array
(
[user_name] => xia***
)
[11] => Array
(
[user_name] => hfa***
)
)