Perfect solution for intercepting Chinese characters without garbled characters-PHP string functions (utf8, GBK, and GB2312 supported)

Source: Internet
Author: User
Perfect solution for intercepting Chinese characters without garbled characters-PHP string functions (utf8, GBK, and GB2312 supported, perfect solution for intercepting Chinese characters without garbled characters-PHP string function: 1. string functions encoded by GB2312 and GBK are perfectly used to extract Chinese characters without garbled characters-PHP string functions (utf8, GBK, and GB2312 are supported)

We mainly explain how to intercept Chinese strings in utf8, GBK, and GB2312, which perfectly solves the problem of intercepting Chinese characters without garbled characters-PHP string function:

1. extract GB2312 and GBK encoded strings

Function: Intercept a GB2312 or GBK-encoded string, starting from the first character. The two lengths represent one Chinese character.
$ Str ---- capture the source string
$ Len ---- truncation length (2 represents a Chinese character)

PS: This function cannot be used to encode UTF-8 strings, and garbled characters may occur.

Function splitStr ($ str, $ len)
{
If ($ len <= 0)
{
Return false;
}
Else
{
$ SLen = strlen ($ str );
If ($ len> = $ sLen)
Return $ str;
Else
{
For ($ I = 0; $ I <($ len-1); $ I ++)
{
If (ord (substr ($ str, $ I, 1)> 0xa0)
$ I ++;
}

If ($ I >=$ len)
Return substr ($ str, 0, $ len );
Elseif (ord (substr ($ str, $ I, 1)> 0xa0)
Return substr ($ str, 0, $ len-1 );
Else
Return substr ($ str, 0, $ len );
}
}
}

2. extract GB2312 and GBK encoded strings

Function: truncates GB2312 and GBK encoded strings. you can set the truncation position and length. The two lengths represent one Chinese character.
$ Str ---- capture the source string
$ Start ---- start position. it cannot be blank and starts from 1.
$ Len ---- truncation length (2 represents a Chinese character). if it is null, it is truncated to the end of the string.

PS: This function cannot be used to encode UTF-8 strings, and garbled characters may occur.

Function substr_for_gb2312 ($ str, $ start, $ len = null)
{
$ Totlelength = strlen ($ str );

// Special case
If ($ len = null) $ len = $ totlelength;
If ($ len = 0) return "";
If ($ len >=$ totlelength & $ start = 0) return $ str;
If ($ start> $ totlelength) return "";

// Analyze $ start
If ($ start <0) // when $ start <0, the position is converted to $ start> 0.
{
If (abs ($ start) >=$ totlelength)
$ Start = 0;
Else
$ Start = $ totlelength-abs ($ start );
}

// Determine the start position. when the start position is split into a Chinese character, the returned value contains this Chinese character.
If ($ start> 0)
{
$ I = $ start-1;
$ Flag =-1;
While ($ I> = 0)
{
If (ord (substr ($ str, $ I, 1)> 160)
{
$ Flag =-1 * $ flag;
}
Else break;
$ I -;
}
If ($ flag = 1)
{
$ Start = $ start-1;
$ Len ++; // ensure no displacement.
}
}

$ Str = substr ($ str, $ start); // removes the $ start character before the string $ str
$ Totlelength = strlen ($ str );

// Determine the end position. when the delimiter splits a Chinese character, the returned value does not contain this Chinese character.
If ($ len <0) $ len = $ totlelength-abs ($ len );
If ($ len <= 0) return "";
$ I = min ($ len, $ totlelength );
$ I -;
$ Flag =-1;
While ($ I> = 0)
{
If (ord (substr ($ str, $ I, 1)> 160)
{
$ Flag =-1 * $ flag;
}
Else break;
$ I -;
}

If ($ flag = 1 )?? ? $ Len = $ len-1;
$ Subit = substr ($ str, 0, $ len );

Return $ subit;
}

3. truncate a UTF-8, GB2312, or GBK-encoded string.

Function: Intercept a UTF-8, GB2312, or GBK-encoded string. the string starts from the first character. one length indicates a Chinese character.
$ Sourcestr ---- capture the source string
$ Cutlength ---- truncation length (word count)

PS: This function is omnipotent, but it consumes more resources than the first two.

Function substr_for_utf8 ($ sourcestr, $ cutlength)
{
$ Returnstr = ";
$ I = 0;
$ N = 0;
$ Str_length = strlen ($ sourcestr );?? ? // Number of bytes of the string
While ($ n <$ cutlength) and ($ I <= $ str_length ))
{
$ Temp_str = substr ($ sourcestr, $ I, 1 );
$ Ascnum = Ord ($ temp_str); // Obtain the ascii code of the $ I character in the string
If ($ ascnum> = 224) // if the ASCII bit height is 224,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 3); // count three consecutive characters as a single character according to the UTF-8 encoding specification
$ I = $ I + 3; // The actual Byte count is 3.
$ N ++; // string length meter 1
}
Elseif ($ ascnum> = 192) // if the ASCII bit height is 192,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 2); // Count 2 consecutive characters as a single character according to the UTF-8 encoding specification
$ I = $ I + 2; // The actual Byte count is 2.
$ N ++; // string length meter 1
}
Elseif ($ ascnum >=65 & $ ascnum <= 90) // if it is a capital letter,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 1 );
$ I = $ I + 1; // The actual number of bytes is still counted as 1
$ N ++; // consider the overall appearance. uppercase letters are counted as a high character.
}
Else // In other cases, including lower-case letters and halfwidth punctuation marks,
{
$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 1 );
$ I = $ I + 1 ;?? ? // The actual number of bytes is 1.
$ N = $ n + 0.5 ;?? ? // Lower-case letters, Halfwidth punctuation, and half-width upper-limit characters...
}
}

If ($ str_length> $ cutlength)
{
$ Returnstr = $ returnstr. "…";? ? // When the length is exceeded, add a ellipsis at the end.
}

Return $ returnstr;
}

Finally, you can write a program to call it. for example:

$ A = "we will write Hello world! This is the simplest program .";
Echo $ ."
";
$ A = substr_for_utf8 ($ a, 4 );
Echo $ ."
";
?>

How is it? Good, so come and try O (∩ _ ∩) O haha ~

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.