Topic on intercepting strings in PHP

Source: Internet
Author: User
1. Intercept the gb2312 Chinese string <? Php <? PHP // extract the Chinese string function mysubstr ($ STR, $ start, $ Len) {$ tmpstr = ""; $ strlen = $ start + $ Len; for ($ I = 0; $ I <$ strlen; $ I ++) {If (ord (substr ($ STR, $ I, 1)> 0xa0) {$ tmpstr. = substr ($ STR, $ I, 2); $ I ++;} else $ tmpstr. = substr ($ STR, $ I, 1);} return $ tmpstr ;}?> 2. truncate a UTF-8 encoded multi-byte string <? Php <? PHP // intercept the utf8 string function utf8substr ($ STR, $ from, $ Len) {return preg_replace ('# ^ (?: [\ X00-\ x7f] | [\ xc0-\ xFF] [\ X80-\ xbf] +) {0 ,'. $ from. '}'. '((?: [\ X00-\ x7f] | [\ xc0-\ xFF] [\ X80-\ xbf] +) {0 ,'. $ Len. '}). * # s', '$ 1', $ Str) ;}?> 3. Chinese Character truncation functions supported by UTF-8 and gb2312 <? Php <? PHP/* Utf-8, gb2312 are supported by the Chinese character truncation function cut_str (string, truncation length, start length, encoding ); the default encoding start length is UTF-8. The default value is 0 */function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 ') {if ($ code = 'utf-8 ') {$ Pa = "/[\ 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] /"; preg_match_all ($ Pa, $ string, $ t_string ); If (count ($ t_string [0])-$ Start> $ sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... "; return join ('', array_slice ($ t_string [0], $ start, $ sublen);} else {$ start = $ start * 2; $ sublen = $ sublen * 2; $ strlen = strlen ($ string); $ tmpstr = ''; For ($ I = 0; $ I <$ strlen; $ I ++) {if ($ I >=$ start & $ I <($ start + $ sublen) {If (ord (substr ($ string, $ I, 1)> 129) {$ tmpstr. = substr ($ string, $ I, 2);} else {$ tmpstr. = substr ($ string, $ I, 1) ;}if (ord (substr ($ string, $ I, 1)> 129) $ I ++ ;} if (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "... "; return $ tmpstr ;}}$ STR =" string to be intercepted by ABCD "; echo cut_str ($ STR, 8, 0, 'gb2312');?> 4. bugfree character truncation function <? Php <? PHP/*** @ package bugfree * @ version $ ID: functionsmain. inc. PHP, V 1.32 11:38:37 wwccss exp $ *** return part of a string (enhance the function substr ()) ** @ author Chunsheng Wang <wwccss@263.net> * @ Param string $ string the string to cut. * @ Param int $ length the length of returned string. * @ Param booble $ append whether APPEND "... ": false | true * @ return string the cutted string. */ Function syssubstr ($ string, $ length, $ append = false) {If (strlen ($ string) <= $ length) {return $ string;} else {$ I = 0; while ($ I <$ length) {$ stringtmp = substr ($ string, $ I, 1); If (ord ($ stringtmp)> = 224) {$ stringtmp = substr ($ string, $ I, 3); $ I = $ I + 3;} elseif (ord ($ stringtmp) >= 192) {$ stringtmp = substr ($ string, $ I, 2); $ I = $ I + 2;} else {$ I = $ I + 1 ;} $ stringlast [] = $ stringtmp;} $ s Tringlast = implode ("", $ stringlast); if ($ append) {$ stringlast. = "... ";}return $ stringlast ;}$ string =" codebit.cn -- simple, brilliant, General "; $ length =" 18 "; $ append = false; echo syssubstr ($ string, $ length, $ append);?>
Related Article

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.