Substr-PHP-Chinese/English mixed-rank string truncation function

Source: Internet
Author: User
RT is used to find a function to intercept string in both Chinese and English. substr (), mb_substr (), and iconv_substr () are not very powerful. the user-defined functions released on the Internet have also tried some, no easy to use .! In fact, my main purpose is to cut out several lines of the word length is exactly the same... RT is used to find a function to intercept string in both Chinese and English. substr (), mb_substr (), and iconv_substr () are not very powerful. the user-defined functions released on the Internet have also tried some, no easy-to-use. =!
In fact, my main purpose is to cut out several lines of words with the same length, so please try again ~

Supplement:
The following is the code I used for testing. it seems that the first and second answers have garbled characters ~
PS: If you want to answer a question, you can first test the small text in my section, and then submit the answer ~

 ';}

Reply content:

RT is used to find a function to intercept string in both Chinese and English. substr (), mb_substr (), and iconv_substr () are not very powerful. the user-defined functions released on the Internet have also tried some, no easy-to-use. =!
In fact, my main purpose is to cut out several lines of words with the same length, so please try again ~

Supplement:
The following is the code I used for testing. it seems that the first and second answers have garbled characters ~
PS: If you want to answer a question, you can first test the small text in my section, and then submit the answer ~

 ';}

I think what you need ismb_strimwidthThis function, let's look at its definition http://cn2.php.net/manual/en/function...

I used a disgusting solution. Assume that your string is UTF-8 encoded, you need to convert it to gbk.

$gbk_str = iconv('UTF-8', 'GBK', $raw);

Next, substr truncates the $ gbk_str. the gbk-encoded Chinese character is two bytes, and the English word is one, so it is just right.

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.