A method of English-Chinese mixed-line string interception in PHP

Source: Internet
Author: User

The Chinese and English mix counts, intercepts, does not use the custom function, but uses PHP's MB extension, uses the original ecology PHP function to handle the string interception easily.

First, the following functions are used to intercept strings. Mb_strwidth ($STR, $encoding) returns the width of the string $str the string to be evaluated $encoding the encoding to use, such as UTF8, Gbkmb_strimwidth ($str, $start, $width, $tail, $encoding) intercept strings by width

$str the string to intercept $start from, the default is 0$width to intercept the width $tail appended to the string that is behind the intercept string, commonly used is ... $encoding the encoding to use

Example:

    1. /**
    2. * UTF8 encoding format
    3. * 1 Chinese takes 3 bytes
    4. * We want 1 Chinese to occupy 2 bytes,
    5. * Because the width of the 2 English letters occupy the position equivalent to 1 Chinese
    6. */
    7. Test string
    8. $STR = ' aaaa ah ah aaaa ah ah aaa ';
    9. echo strlen ($STR); Use only strlen output to 25 bytes
    10. You must specify the encoding, or you will use PHP's internal code mb_internal_encoding () to view the inner code
    11. Use the Mb_strwidth output string with a width of 20 using UTF8 encoding
    12. Echo mb_strwidth ($str, ' UTF8 ');
    13. Only a width greater than 10 is intercepted.
    14. if (Mb_strwidth ($str, ' UTF8 ') >10) {
    15. Set here to intercept from 0, take 10 append ..., use UTF8 encoding
    16. Note the additional ... will also be counted within the length of the
    17. $str = mb_strimwidth ($str, 0, ' ... ', ' UTF8 ');
    18. }
    19. Finally output aaaa ah ... 4 A, 4, 1, 2, 3, 3, 4+2+3=9.
    20. is not very simple ah, some people say why is 9 not 10 of them?
    21. Because just "ah" behind or "ah", Chinese 2, 9+2=11 beyond the set, so remove one is 9
    22. Echo $str;
Copy Code

Other string intercept functions: Mb_strlen ($STR, $encoding) returns the length of the string $str the encoding to use for the string $encoding to be evaluated

Mb_substr ($str, $start, $length, $encoding) intercept the string $str the string to intercept $start from where to intercept $length intercept how long $encoding used the encoding actually these 2 functions and strlen () , substr () is like, the only difference is that you can set the encoding.

An example of the above two string intercept functions.

    1. /**
    2. * UTF8 encoding format
    3. * 1 Chinese takes 3 bytes
    4. */
    5. $STR = ' aa12 ah aa ';
    6. echo strlen ($STR); Direct output Length of 9
    7. The output length is 7, why is it 7?
    8. Note that after the code is set, either Chinese or English, each length is 1
    9. A a 1 2 ah a A
    10. 1+1+1+1+1+1+1 = 7
    11. Is it just a 7-character?
    12. Echo Mb_strlen ($str, ' UTF8 ');
    13. Same mb_substr, too.
    14. I only want 5 characters now
    15. Echo mb_substr ($str, 0, 5, ' UTF8 '); Output aa12.
Copy Code

MB Extension library also has a lot of practical functions, this is not introduced here, you can refer to the following PHP manual for the relevant content.

  • 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.