Jquery intercepts Chinese strings

Source: Internet
Author: User

Function cut_str ($ 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,

{

// According to the UTF-8 encoding specification, count 3 consecutive characters as a single character

$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 3 );

$ I = $ I + 3; // The actual byte count is 3.

$ N ++; // string length Meter 1

}

Elseif ($ ascnum> = 192) // If the ASCII bit height is 192,

{

// According to the UTF-8 encoding specification, Count 2 consecutive characters as a single character

$ Returnstr = $ returnstr. substr ($ sourcestr, $ I, 2 );

$ 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 and halfwidth punctuation and half-height character width...

}

}

If ($ str_length> $ cutlength ){

$ Returnstr = $ returnstr. "..."; // when the length is exceeded, add a ellipsis at the end.

}

Return $ returnstr;

}

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.