PHPUTF8 Chinese character truncation function code. Copy the code as follows :? Php * UTF-8 Chinese character truncation program * $ str123 this is the test string; $ str1 (); echosubUTF8str ($ str,). br; echosubUTF8str ($ str)
The code is as follows:
/* UTF-8 Chinese character truncation program */
$ Str = "123 this is a test string ";
$ Str1 = "()()";
Echo subUTF8str ($ str, 0, 3 )."
";
Echo subUTF8str ($ str, 0, 4 )."
";
Echo subUTF8str ($ str1, 0, 4 )."
";
Echo subUTF8str ($ str1, 0, 10 )."
";
Function subUTF8str ($ str, $ start = 0, $ length = 80 ){
$ Cur_len = 0; // string length understood by a person
$ All_len = strlen ($ str); // machine understanding string length
If ($ length> $ all_len)
{
Return $ str;
}
For ($ I = 0; $ I <$ all_len ;)
{
If ($ cur_len = $ start)
{
Break;
}
If (ord ($ str [$ I])> 127)
{
$ I + = 3;
} Else {
$ I + = 1;
}
$ Cur_len ++;
}
$ Start_pos = $ I;
$ Temp_pos = $ cur_len;
For (; $ cur_len-$ temp_pos <$ length ;)
{
If ($ I >=$ all_len)
Break;
If (ord ($ str [$ I])> 127)
{
$ I + = 3;
} Else {
$ I + = 1;
}
$ Cur_len ++;
}
$ End_pos = $ I;
Return substr ($ str, $ start_pos, $ end_pos );
}
?>
In fact, PHP native has a multi-charset character truncation scheme, the amount, so it looks like this... pretty ..
In the Multibyte String Functions function family,
String mb_substr (string $ str, int $ start [, int $ length [, string $ encoding]) is used to intercept strings.
Int mb_strlen (string $ str [, string $ encoding]) returns the string length.
....
For more information, see The PHP Manual.
The http://www.bkjia.com/PHPjc/326024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326024.htmlTechArticle code is as follows :? Php/* UTF-8 Chinese character truncation program */$ str = "123 this is the test string"; $ str1 = "()"; echo subUTF8str ($ str ). "br"; echo subUTF8str ($ str, 0,4 )...