PHP UTF8 Chinese Character truncation program code-PHP source code

Source: Internet
Author: User
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 );
}

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.