Publish a newly compiled brute force version/gentle version of the Chinese truncation function _ PHP Tutorial

Source: Internet
Author: User
Publish a newly compiled brute force version of the gentle version of the Chinese truncation function. It is recommended to use the brute-force version, which is safe and reliable. the gentle version is more efficient in programming. the basic principle is to correct the off and len may be misplaced. the gentle version is from the off and recommended to use the violent version, which is safe and reliable; the gentle version is more efficient from the perspective of programming. haha

The basic principle is to correct off, len may be misplaced, gentle version is from off backward lookup, find the first character <0xa0 is considered as a common character, the search is complete, determine whether there is a dislocation based on the number of times...

/**
* @ Brief simple and efficient string truncation function (supports CJK characters)
*
* It simply determines the ASCII value of the high part and can cope with the vast majority of regular Chinese and English strings.
* UTF-8 encoding of 4 or 3 bytes is not supported.
*
* Key point: modify the off value/len value of the dislocation in the double byte (pay attention to the intention that the default value of $ len is-1)
* Similar to substr (), the low position (starting from 0x40) of GBK code may be faulty.
*/
Function my_substr ($ str, $ off, $ len =-1)
{
$ Mlen = strlen ($ str );

/* Step 4: parameter security check and correction */
If ($ off <0)
$ Off = $ mlen;
If ($ off> $ mlen)
$ Off = 0;

/* Step 2: $ off correction, reverse lookup */
If ($ off> 0)
{
$ Fix = $ off;
$ Mb = false;
Do
{
$ Ch = ord ($ str {$ fix --});
If ($ ch <0x80)
Break;
$ Mb = true;
}
While ($ fix );

If ($ mb)
{
$ Fix = ($ off-$ fix );
If ($ fix & 1)
{
$ Off --;
$ Len;
}
}
}

/* Step 2: $ len correction, same as above */
If ($ len <= 0 | ($ len $ off) >=$ mlen)
{
$ Len = $ mlen-$ off;
}
Else
{


Secure and reliable. the gentle version is more efficient from the programming perspective. The basic principle is to correct off, len may be misplaced, and the gentle version is to reverse from off...

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.