设置字符集后怎么还乱码

WBOY
Release: 2016-06-23 14:27:07
Original
836 people have browsed it

设置字符集后还乱码

$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 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;
Copy after login
判断*****

按照这种写法,为什么foreach循环$v['user_name']会少一个数??
foreach($load_list as $k=>&$v){
//print_r($v);
//exit;
$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);
}
}
$t=&$v['user_name'];

}

为什么会少一个数了,测试很多遍都不行,实在不行了,才来求助

echo base64_encode($v['user_name']);
贴出结果!

你不给出问题现场,如何知道是什么原因?

$load_list是一个二维数组,其中的$v['user_name']有以下数据
xiaodg,x42466,etea,na0617,kimo,留影,vian22,qingyu,jiangliman,xiaobu,xiaobu,hfa818,实现的结果将两个字符后面的用*代替,下面是实现*代码,
foreach($load_list as $k=>&$v){
//print_r($v);
//exit;
$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);
}
}
$t=&$v['user_name'];

}

这是assign赋值:
$GLOBALS['tmpl']->assign("load_list",$load_list);

然后在页面用samrty显示:
{foreach from="$load_list" item="load"}


{$load.user_name}



但是显示的结果是xiaodg没有了,从第二个x42466开始显示
这是结果图:

第一个数没有了

你自己写错了,不能怪算法!
$t=&$v['user_name'];
应该是 $v['user_name'] = $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);
Copy after login
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***        ))
Copy after login

谢谢版主,我自己调试测试了很多,自己也写了算法,终究是结果不成功,才来论坛发帖,感谢版主细心指导,谢谢!!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template