PHP Chinese string intercept function

xiaoxiao2021-03-31  231

The first is the 9CBS Forum PHP Bamboo Xuzuning () Boss, support GB2312, GBK, BIG three coding.

The following is the code: $ len = 19; $ text = "How to display a long title only in front of the news, use ......"; Echo Strlen ($ text) <= $ LEN? $ TEXT: (Substr ($ text, 0, $ g .chr (0). "...");

CHR (0) is not Nullnull, and the value of chr (0) is 0. Indicates that 16 is 0x00, indicating that the binary is 00000000 Although chr (0) does not show anything, but he is a character. When the Chinese character is truncated, he always pulled the other characters behind him as a Chinese character, depending on the coding rules. This is the reason why there is garbled. The value of 0x81 to 0xFF and 0x00 is always displayed as "empty" according to this feature, and after the result of Substr, it can prevent garbled.

Note: Code first byte second byte GB2312 0xa1-0xf7 0xa1-0xfegbk 0x81-0xfe 0x81-0xfe 0x40-0x7ebig5 0xa1-0xf7 0x81-0xfe 0x40-0x7e

Secondly, this is online search, support UTF-8 encoding, the original author is not detailed: Function Substring_utf8 ($ Str, $ Start, $ LENTH) {$ LEN = Strlen ($ Str); $ R = array (); $ n = 0 $ M = 0; for ($ I = 0; $ i ) {$ x = Substr ($ Str, $ I, 1); $ a = base_convert (ORD ($ x), 10, 2); $ a = substr ('00000000'. $ A, -8); if ($ n <$ start) {IF (Substr ($ A, 0, 1) == 0) {} Elseif (Substr ($ A, 0, 3) == 110) {$ I = 1;} elseif (Substr ($ A, 0, 4) == 1110) {$ i = 2;} $ n ;} else {= (Substr ($ A, 0, 1) == 0) {$ R [] = Substr ($ STR, $ I, 1);} elseif (Substr ($ A, 0, 3) == 110) {$ R [] = Substr ($ STR, $ I, 2); $ I = 1;} elseif (Substr ($ A, 0, 4) == 1110) {$ R [] = SUBSTR ($ STR, $ I, 3) ; $ I = 2;} else { $ R [] = '';} if ( $ m> = $ LENTH) {Break;}}} Return $ R;} // end substring_utf8;} // end string # Because this function returns an array Therefore, therefore to match the Join function to display the string: #join ('', substring_utf8 ($ str, $ start, $ get)); # On the page, you can even connect a "..." after this statement. ... "

转载请注明原文地址:https://www.9cbs.com/read-130944.html

New Post(0)