This article mainly introduces php's method of intercepting Chinese characters by character without garbled characters, and involves php's skills in operating Chinese and English strings, which is of great practical value, for more information, see the next article. This article mainly introduces php's method of intercepting Chinese characters without garbled characters. it involves php's skills in operating Chinese and English strings. it is very useful. For more information, see
This example describes how php intercepts Chinese characters without garbled characters. Share it with you for your reference. The specific implementation method is as follows:
The number of characters in $ str, resulting in unnecessary while loops. ($ offset <$ length limit) function utf8sub ($ str, $ len) {if ($ len <= 0) {return ;}$ res = ""; $ offset = 0; $ chars = 0; $ length = strlen ($ str ); while ($ chars <$ len & $ offset <$ length) {$ hign = decbin (ord (substr ($ str, $ offset, 1 ))); if (strlen ($ hign) <8) {$ count = 1;} elseif (substr ($ hign, 110) = "") {$ count = 2 ;} elseif (substr ($ hign, 1110) = "11110") {$ count = 3;} elseif (substr ($ hign,) = ") {$ count = 4;} el Seif (substr ($ hign, 111110) = "1111110") {$ count = 5;} elseif (substr ($ hign,) = ") {$ count = 6;} $ res. = substr ($ str, $ offset, $ count); $ offset + = $ count; $ chars + = 1;} return $ res;} function utf8sub1 ($ str, $ len) {$ chars = 0; $ res = ""; $ offset = 0; $ length = strlen ($ str ); while ($ chars <$ len & $ offset <$ length) {$ hign = decbin (ord (substr ($ str, $ offset, 1 ))); if (strlen ($ hign) <8) {$ count = 1;} elseif ($ hign & "11100000" = "11 000000 ") {$ count = 2;} elseif ($ hign &" 11110000 "=" 11100000 ") {$ count = 3 ;} elseif ($ hign & "11111000" = "11110000") {$ count = 4;} elseif ($ hign & "11111100" = "11111000 ") {$ count = 5;} elseif ($ hign & "11111110" = "11111100") {$ count = 6;} $ res. = substr ($ str, $ offset, $ count); $ chars ++; $ offset + = $ count;} return $ res ;} $ a = "Chinese ah people hdj"; echo utf8sub ($ a, 5);?>
The above is the php method for intercepting Chinese characters without garbled characters _ php skills. For more information, see PHP Chinese website (www.php1.cn )!