<?PHPHeader("Content-type:text/html;charset=utf-8");$str= "1 Chinese characters utf-8 Code";/** * $b utf-8 with 3, gb2312 with 2*/functionTest$str,$b=3){ $length=strlen($str); $rets= ' '; for($i= 0;$i<$length;$i++){ if(Ord(substr($str,$i, 1)) >128){ $rets[] =substr($str,$i,$b); $i+=$b-1; }Else{ $rets[] =substr($str,$i, 1); } } return implode(‘‘ ,Array_reverse($rets)); } $test= Test ($str);Var_dump($test);/**string (24) "8-FTU 1" in the word "Code series "*/
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP$a= ' abcd min Love Army son ';//the test string, which contains the Chinese language///test output below, because when encoded as Utf-8, a Chinese character occupies 3 lengths//echo strlen (' min ');$b= ' ';$i=0; while($i<strlen($a)){ if(Ord(substr($a,$i, 1)) >0xa0)//The Ord function determines whether the string is Chinese { $b=substr($a,$i, 3).$b;//This is a very important sentence, reverse! $i+=3; } Else { $b=substr($a,$i, 1).$b; $i++; }}Echo $b;?></body><?php
Header ("Content-type:text/html;charset=utf-8");
$STR = "1 Chinese characters utf-8 Code";
/**
* $b utf-8 with 3, gb2312 with 2
*/
Function Test ($str, $b =3) {
$length = strlen ($STR);
$rets = ";
for ($i = 0; $i < $length; $i + +) {
if (Ord (substr ($str, $i, 1)) >128) {
$rets [] = substr ($str , $i, $b);
$i + = $b-1;
}else{
$rets [] = substr ($str, $i, 1);
}
}
Return implode (", Array_reverse ($rets));
}
$test = Test ($STR);
Var_dump ($test);
/**
String (24) "Code 8-FTU 1"
*/