Php implements various Chinese character string truncation problems,

Source: Internet
Author: User

Php implements various Chinese character string truncation problems,

If you use php to intercept Chinese strings, various problems may occur. A brief summary is provided. The problem in this article has not been solved yet. If you have solved the problem, please kindly advise.

 

1 <? Php 2 3 header ('content-Type: text/html; charset = UTF-8 '); // The page uses UTF-8 encoding 4 // header ('content-Type: text/html; charset = gbk '); // The page uses gbk encoding 5 6/** 7 * for Chinese interception without garbled 8 * @ param $ str: string 9 * @ param $ start: start position 10 * @ param $ length: truncation length 11 * @ return null | string 12 */13/* 14 test failed 15 first function: chinese string truncation will garbled 16 */17/* 18 function GbSubstring ($ string, $ start, $ length) {19 if (strlen ($ string)> $ length) {20 $ str = null; 21 $ l En = $ start + $ length; 22 23 for ($ I = $ start; $ I <$ len; $ I ++) {24 if (ord (substr ($ string, $ I, 1)> 0Xa0) {25 $ str. = substr ($ string, $ I, 2); 26 $ I ++; 27 28} else {29 $ str. = substr ($ str, $ I, 1); 30} 31} 32 return $ str. '.... '; 33} else {34 return $ string; 35} 36 37} 38 39 $ str1 = GbSubstring ('hahaha Hi Ah', 1, 3); 40 echo $ str1; 41 */42 43/* 44 second function test successful 45 */46/** 47 * Applicable to UTF-8 48 * @ param $ str 49 * @ param int $ start 5 0 * @ param $ length 51 * @ param string $ charset 52 * @ param bool | true $ suffix 53 * @ return string | void 54 */55/* 56 function msubstr ($ str, $ start = 0, $ length, $ charset = "UTF-8", $ suffix = true) 57 {58 if (function_exists ("mb_substr") 59 return mb_substr ($ str, $ start, $ length, $ charset); 60 elseif (function_exists ('iconv _ substr') {61 return iconv_substr ($ str, $ start, $ length, $ charset ); 62} 63 $ re ['ut F-8 '] = "/[x01-x7f] | [xc2-xdf] [x80-xbf] | [xe0-xef] [x80-xbf] {2} | [xf0-xff] [x80-xbf] {3 }/"; 64 $ re ['gb2312'] = "/[x01-x7f] | [xb0-xf7] [xa0-xfe]/"; 65 $ re ['gbk'] = "/[x01-x7f] | [x81-xfe] [x40-xfe]/"; 66 $ re ['big5'] = "/[x01-x7f] | [x81-xfe] ([x40-x7e] | xa1-xfe])/"; 67 preg_match_all ($ re [$ charset], $ str, $ match); 68 $ slice = join ("", array_slice ($ match [0], $ start, $ length); 69 if ($ suffix) return $ slice. "... "; 70 return $ slice; 71} 72 73 echo msubstr ('hahaha hello, Ah', 1, 3); 74 */75 76/* 77 third function: test Error 78 */79/** 80 * @ param $ string 81 * @ param $ start 82 * @ param $ length 83 * @ return string 84 */85/* 86 function substr2 ($ string, $ start, $ length) 87 {88 $ len = strlen ($ string); 89 if ($ len> $ length) 90 {91 $ str = ''; 92 $ len1 = $ start + $ length; // capture to the position of the original string 93 for ($ I = $ start; $ I <$ len1; $ I ++) 94 {95 if (o Rd (substr ($ string, $ I, 2)> 0xa0) // in ASCII, 0xa0 indicates the start of the Chinese character 96 {97 $ str. = substr ($ string, $ I, 2); 98 $ I ++; 99} 100 else101. {102 $ str. = substr ($ string, $ I, 1); 103} 104} 105 return $ str. '... '; 106} 107 else108 {109 return $ string; 110} 111} 112 echo substr2 ('hahaha hi Ah', 1, 3 ); 113 */114 115/* 116 fourth function: Test Error 117 */118/* 119 function chinesesubstr ($ str, $ start, $ len) {120 $ tmpstr = null; 121 $ strlen = $ start + $ len; 1 22 for ($ I = 0; $ I <$ strlen; $ I +++) {123 if (ord (substr ($ str, $ I, 1)> 0xa0) {124 $ tmpstr. = substr ($ str, $ I, 2); 125 $ I ++; 126} else {127 $ tmpstr. = substr ($ str, $ I, 1); 128} 129} 130 return $ tmpstr; 131} 132 $ str = 'hahaha Hi Ah'; 133 echo chinesesubstr ($ str, 134); 135 */136 137/* 138 system function mb_substr test successful 139 */140 echo mb_substr ('hahaha hi Ah, '141, "UTF-8");?>

 

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.