Efficient Chinese string truncation Functions

Source: Internet
Author: User
When using the traditional string truncation function substr in PHP to process strings containing Chinese characters, Chinese characters are cut off. When PHP extension libraries can be used, we can use mb_substr instead. However, it is difficult for the extension library to be connected. in Linux, PHP needs to be re-compiled. Sometimes it cannot be done, not to mention its many redundant functions.
You can see many functions that implement this function on the network. However, many algorithms use loop judgment, which is very inefficient when strings are large.
Here we will introduce two efficient functions: c_substr and m_substr. Their usage is exactly the same as that of substr and mb_substr. The difference is that c_substr is calculated by byte, that is, the length of a Chinese character is 2; m_substr is calculated by word, that is, the length of a Chinese character is 1. Optional as needed.

Code: function c_substr ($ STR, $ start = 0 ){
$ CHR = CHR (127 );
$ P = array ("/[/x81-/xfe] ([/x81-/xfe] | [/X40-/xfe])/", "/[/x01-/x77]/");
$ R = array ("","");
If (func_num_args ()> 2)
$ End = func_get_arg (2 );
Else
$ End = strlen ($ Str );
If ($ start <0)
$ Start + = $ end;

If ($ Start> 0 ){
$ S = substr ($ STR, 0, $ start );
If ($ s [strlen ($ S)-1]> $ ch ){
$ S = preg_replace ($ P, $ R, $ S );
$ Start + = strlen ($ S );
}
}
$ S = substr ($ STR, $ start, $ end );
$ End = strlen ($ S );
If ($ s [$ end-1]> $ ch ){
$ S = preg_replace ($ P, $ R, $ S );
$ End + = strlen ($ S );
}
Return substr ($ STR, $ start, $ end );
}

Function m_substr ($ STR, $ start ){
Preg_match_all ("/[/X80-/xFF]?. /", $ STR, $ AR );
If (func_num_args ()> = 3 ){
$ End = func_get_arg (2 );
Return join ("", array_slice ($ ar [0], $ start, $ end ));
} Else
Return join ("", array_slice ($ ar [0], $ start ));
}

Windix Feng has also rewritten A UTF-8 that is applicable and also documented together. For later use. Function utf8_substr2 ($ STR, $ start ){
/*
The UTF-8 version of substr (), for people who can't use mb_substr () like me.
Length is not the Count of bytes, but the Count of UTF-8 characters

Author: windix Feng
Bug report to: windix (AT) 263.net, http://www.douzi.org/blog

-History-
1.0 initial version
2.0 use preg instead of strcmp and cycles, speed up!
*/

Preg_match_all ("/[/x01-/x7f] | [/xc2-/xdf] [/X80-/xbf] |/xe0 [/xa0-/xbf] [/X80- /xbf] | [/xe1-/XeF] [/X80-/xbf] [/X80-/xbf] |/xf0 [/x90-/xbf] [/X80 -/ xbf] [/X80-/xbf] | [/xf1-/xf7] [/X80-/xbf] [/X80-/xbf] [/X80-/xbf]/", $ STR, $ AR );

If (func_num_args ()> = 3 ){
$ End = func_get_arg (2 );
Return join ("", array_slice ($ ar [0], $ start, $ end ));
} Else {
Return join ("", array_slice ($ ar [0], $ start ));
}
}

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.