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!!