New appearance of old questions: Questions about intercepting Chinese strings in PHP

Source: Internet
Author: User
Old question new appearance: PHP intercepts Chinese strings. read old question new appearance: PHP intercepts Chinese strings. the following code is used for GB2312 encoding. intercepting Chinese strings is a headache in PHP, the solution is to determine whether it is a dubyte character based on whether the value is greater than or equal to 128 to avoid garbled characters. But Chinese and English mix, special symbols and other issues "> <LINKhref =" http://www.ph

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 a double byte character based on whether the value is greater than or equal to 128, to avoid garbled characters. However, problems such as Chinese and English mixing and special symbols always exist. now we write a comprehensive one for your reference only:

Program description:
1. the len parameter is based on Chinese characters. 1len is equal to 2 English characters.
2. if the magic parameter is set to false, the Chinese character and English character count are equivalent.
3. special character strings that have been encoded with htmlspecialchars ()
4. correct handling of the GB2312 entity character mode)

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 starting position as incorrect
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;
$ Length + = 6;
$ I + = 5;
$ Realnum ++;
If ($ magic)
{
$ Alen ++;
}
}
Else if (preg_match ("/& # (\ d +);/I", substr ($ title, $ I, 8), $ match ))
{
$ Cstep = strlen ($ match [0]);
$ Length + = strlen ($ match [0]);
$ I + = strlen ($ match [0])-1;
$ Realnum ++;
If ($ magic)
{
$ Blen ++;
$ Ctype = 1;
}
}
} Else {
If (ord ($ cur) >=128)
{
$ Cstep = 2;
$ Length + = 2;
$ I + = 1;
$ Realnum ++;
If ($ magic)
{
$ Blen ++;
$ Ctype = 1;
}
} Else {
$ Cstep = 1;
$ Length + = 1;
$ Realnum ++;
If ($ magic)
{
If (ord ($ cur) >=65 & ord ($ cur) <= 90)
{
$ Blen ++;
} Else {
$ Alen ++;
}
}
}
}

If ($ magic)
{
If ($ blen * 2 + $ alen) = ($ len * 2) break;
If ($ blen * 2 + $ alen) = ($ len * 2 + 1 ))
{
If ($ ctype = 1)
{
$ Length-= $ cstep;
Break;
} Else {
Break;
}
}
} Else {
If ($ realnum = $ len) break;
}
}

Unset ($ cur );
Unset ($ alen );
Unset ($ blen );
Unset ($ realnum );
Unset ($ ctype );
Unset ($ cstep );

Return substr ($ title, $ start, $ length );
}

Haha, I will post the code under the UTF-8 code later

Related Article

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.