The following code is used for GB2312 encoding. intercepting a Chinese string is a headache in PHP. the solution is to determine whether the value is greater than or equal to 128 to determine whether it is a dubyte character, to avoid garbled characters. However, both Chinese and English are mixed, with special symbols
The following code is used for GB2312 encoding. intercepting a Chinese string is a headache in PHP. the solution is to determine whether the value is greater than or equal to 128 to determine whether it is a dubyte character, to avoid garbled characters. However, titles such as English and Chinese characters and special symbols always exist. now, we write a comprehensive one for your reference only:
Program clarification:
1. the len parameter is based on Chinese characters, and 1len is two English characters.
2. if the magic parameter is set to false, the Chinese character and the English character are treated in the same way, and the correct number of characters is used.
3. it is particularly useful for encoded strings using htmlspecialchars ().
4. can accurately process the GB2312 entity character mode (bytes)
Program code:
Function FSubstr ($ title, $ start, $ len = '', $ magic = true)
{
/**
* Powered by Smartpig
* Mailto: d.einstein@263.net
*/
$ Length = 0;
If ($ len = '') $ len = strlen ($ title );
// Judge the initial position as inaccurate
If ($ start> 0)
{
$ Cnum = 0;
For ($ I = 0; $ I <$ start; $ I)
{
If (ord (substr ($ title, $ I, 1) >= 128) $ cnum;
}
If ($ cnum % 2! = 0) $ start --;
Unset ($ cnum );
}
If (strlen ($ title) <= $ len) return substr ($ title, $ start, $ len );
$ Alen = 0;
$ Blen = 0;
$ Realnum = 0;
For ($ I = $ start; $ I {
$ Ctype = 0;
$ Cstep = 0;
$ Cur = substr ($ title, $ I, 1 );
If ($ cur = '&')
{
If (substr ($ title, $ I, 4) = '<')
{
$ Cstep = 4;
$ Length = 4;
$ I = 3;
$ Realnum;
If ($ magic)
{
$ Alen;
}
}
Else if (substr ($ title, $ I, 4) = '> ')
{
$ Cstep = 4;
$ Length = 4;
$ I = 3;
$ Realnum;
If ($ magic)
{
$ Alen;
}
}
Else if (substr ($ title, $ I, 5) = '&')
{
$ Cstep = 5;
$ Length = 5;
$ I = 4;
$ Realnum;
If ($ magic)
{
$ Alen;
}
}
Else if (substr ($ title, $ I, 6) = '"')
{
$ Cstep = 6;
$ Length = 6;
$ I = 5;
$ Realnum;
If ($ magic)
{
$ Alen;
}
}
Else if (substr ($ title, $ I, 6) = ''')
{
$ Cstep = 6;