PHP Simple to handle Chinese and English mixed-row string interception, just 2 lines of code!

Source: Internet
Author: User
Tags format copy count functions mixed string strlen
Mentioned in the Chinese and English mixed row count, interception, we first thought of is ASCII, 16, regular match, loop count.   What I'm sharing today is PHP's MB extension, which teaches you how to handle strings easily.       First to introduce the functions used:   Mb_strwidth ($STR, $encoding) returns the width of the string   $STR the string to be computed   $encoding the encoding to use , such as UTF8, GBK   mb_strimwidth ($str, $start, $width, $tail, $encoding) intercept strings by width   $STR the string to intercept   $start from which position to open The first interception, the default is 0   $width to intercept the width   $tail append to the string behind the intercept string, often ...   $encoding to use the code       below to give you an example demo:   Copy Code <?php/**  * UTF8 encoded format  * 1 Chinese 3 bytes  * We want 1 Chinese to occupy 2 bytes,  * because the width of 2 English letters occupies the same position as 1 Chinese  */ //test string $str = ' aaaa ah aaaa ah ah ah aaa '; echo strlen ($STR); Only strlen output to 25 bytes  //must specify encoding, otherwise it will use PHP's inner Code mb_internal_encoding () can view the inner code//use Mb_strwidth output string width of 20 using UTF8 encoded echo Mb_strwidth ($str, ' UTF8 ');   //Only if the width is greater than 10 to intercept if (Mb_strwidth ($str, ' UTF8 ') >10) {   //Here set the Intercept from 0, Take 10 append ..., using UTF8 coding    //Note appended ... will also be computed to the length     $STR = mb_strimwidth ($str, 0, ' ... ', ' UTF8 ');  //FINAL output aaaa ah ... 4 A, 4, 1, 2 3, 3 4+2+3=9//is not very simple ah, some people say why 9 is not 10? Because the right "ah" behind or "ah", Chinese 2, 9+2=11 exceeded the set, so remove one is 9 echo $str; Copy code         below let me introduce you to some other functions:   Mb_strlen ($STR, $encoding) returns the length of the string   $STR the string to be computed   $e Ncoding uses the encoding   MB_SUBSTR ($STR, $start, $length, $encoding) to intercept the string   $STR the string to intercept   $start where to start intercepting   $l Ength Intercept how long   $encoding use the encoding   in fact, these 2 functions and strlen (), substr () is very similar, the only difference is that you can set the encoding.       Example below:   Copy code <?php/**  * UTF8 encoding format  * 1 Chinese to occupy 3 bytes  */$str = ' aa12 ah aa '; echo strlen ($STR); The direct output length is 9  //The output length is 7, why is 7? Note that after setting the encoding, whether in Chinese or English each length is 1//a a 1 2 ah a //1+1+1+1+1+1+1 = 7//is not exactly 7 characters ah Echo mb_strlen ($str, ' UTF8 ');   The same mb_substr is the same//I just want 5 characters. Echo mb_substr ($str, 0, 5, ' UTF8 '); Output aa12 Ah, copy code.

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.