How to still garbled after setting character set

Source: Internet
Author: User
Characters are garbled after setting the character set

$load _list is a two-dimensional array that takes the first two characters of the user_name value, followed by an asterisk, but is garbled after setting the character set, but Chinese is to display the first two characters, followed by an asterisk
foreach ($load _list as $k =>& $v) {

$temp =& $v [' user_name '];
Determine if there is a Chinese
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]= ' * ';
}


Reply to discussion (solution)

$temp = ' Determine if there is a Chinese ';//Determine if there is a Chinese 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;
Judging * * * *

In this way, why is the Foreach Loop $v[' user_name '] less than one number??
foreach ($load _list as $k =>& $v) {
Print_r ($v);
Exit
$temp = $v [' user_name '];

Determine if there is a Chinese
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 '];

}

Why the number of a few, test many times can not, really not, just to help

Echo Base64_encode ($v [' user_name ']);
Post the results!

You do not give the scene of the problem, how to know what is the reason?

$load _list is a two-dimensional array where $v[' user_name ' has the following data
Xiaodg,x42466,etea,na0617,kimo, photos, Vian22, qingyu,jiangliman,xiaobu,xiaobu,hfa818, the result of the implementation will replace two characters followed by *, the following is the implementation * code,
foreach ($load _list as $k =>& $v) {
Print_r ($v);
//exit;
$temp = $v [' user_name '];

//To determine if there is a Chinese
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 '];

}

This is assign assignment:
$GLOBALS [' Tmpl ']->assign ("Load_list", $load _list); The

is then displayed with Samrty on the page:
{foreach from= $load _list "item=" Load "}


{$load. user_name}



But the result shown is XIAODG, starting from the second x42466
This is the result diagram:

The first number is gone

you wrote it wrong, you can't blame the algorithm!
$t =& $v [' user_name ']; The
should be $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 ' + ' photos '), 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 '];  Determine if there is a Chinese 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        ( C22/>[user_name] = photo        )    [6] = =        array            ([user_name] = via***        )    [7] = = Array        (            [user_name] = qin***        )    [8] = = Array        (            [user_name] = jia*******        )    [9] = = Array        (            [user_name] = xia***        )    [ten] = = Array        (            [user_name]        = xia***)    [11] = > Array        (            [user_name] = hfa***        ))

Thank you moderator, I debug a lot of my own test, I also wrote the algorithm, after all, the results are not successful, just to the forum post, thanks to the moderator carefully guide, thank you!!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.