This article summarizes an example of a better UTF8 Chinese Character truncation program for your reference. This article summarizes an example of a better UTF8 Chinese Character truncation program for your reference.
Script ec (2); script
The simplest character truncation function is to use the substr () provided by php ()
In fact, PHP native has a multi-charset character truncation scheme, the amount, so it looks like this ...??.
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.
However, it only supports numbers and letters and does not support Chinese characters.
Instance
UTF8 Chinese Character Truncation
| 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 );
}
?> |
After improvement, we will paging it to support Chinese characters such as ut8 and gbk.
| The Code is as follows: |
|
|
Function substrs ($ content, $ length ){
If ($ length & strlen ($ content)> $ length ){
If ($ db_charset! = 'Utf-8 '){
$ Retstr = '';
For ($ I = 0; $ I <$ length-2; $ I ++ ){
$ Retstr. = ord ($ content [$ I]) & gt; 127? $ Content [$ I]. $ content [++ $ I]: $ content [$ I];
}
Return $ retstr;
} Else {
Return utf8_trim (substr ($ content, 0, $ length * 3 ));
}
}
Return $ content;
}
Function utf8_trim ($ str ){
$ Len = strlen ($ str );
For ($ I = strlen ($ str)-1; $ I >=0; $ I-= 1 ){
$ Hex. = ''. ord ($ str [$ I]);
$ Ch = ord ($ str [$ I]);
If ($ ch & 128) = 0) return substr ($ str, 0, $ I );
If ($ ch & 192) = 192) return substr ($ str, 0, $ I );
}
Return ($ str. $ hex );
} |