The following code is used for GB2312 encoding. intercepting a Chinese string is a headache in PHP. the solution is to determine whether the value is a double byte character based on whether the value is greater than or equal to 128, to avoid garbled characters. However, problems such as Chinese and English mixing and special symbols always exist. now we have a comprehensive description for your reference only: Program Description: 1. the following code uses the len parameter as a standard for GB2312 encoding. intercepting Chinese strings is a headache in PHP, the solution is to determine whether it is a dubyte character based on whether the value is greater than or equal to 128 to avoid garbled characters. However, problems such as Chinese and English mixing and special symbols always exist. now we write a comprehensive one for your reference only:
Program description:
1. the len parameter is based on Chinese characters. 1len is equal to 2 English characters.
2. if the magic parameter is set to false, the Chinese character and English character count are equivalent.
3. special character strings that have been encoded with htmlspecialchars ()
4. correct handling of the GB2312 entity character mode)
Program code:
Function FSubstr ($ title, $ start, $ len = "", $ magic = true) {/*** powered by Smartpig * mailto: d.einstein@263.net */$ length = 0; if ($ len = "") $ len = strlen ($ title); // you can specify the starting position as incorrect. if ($ start> 0) {$ cnum = 0; for ($ I = 0; $ I <$ start; $ I) {if (ord (substr ($ title, $ I, 1) >= 128) $ cnum ;} if ($ cnum % 2! = 0) $ start --; unset ($ cnum);} if (strlen ($ title) <= $ len) return substr ($ title, $ start, $ len ); $ alen = 0; $ blen = 0; $ realnum = 0; for ($ I = $ start; $ I
") {$ Cstep = 4; $ length = 4; $ I = 3; $ realnum; if ($ magic) {$ alen ;}} else if (substr ($ title, $ I, 5) = "&") {$ cstep = 5; $ length = 5; $ I = 4; $ realnum; if ($ magic) {$ alen ;}} else if (substr ($ title, $ I, 6) = ") {$ cstep = 6; $ length = 6; $ I = 5; $ realnum; if ($ magic) {$ alen ;}} else if (substr ($ title, $ I, 6) = "'") {$ cstep = 6; $ length = 6; $ I = 5; $ realnum; if ($ magic) {$ alen ;}} else if (preg_match ("/& # (\ d);/I", substr ($ title, $ I, 8), $ match )) {$ cstep = strlen ($ match [0]); $ length = strlen ($ match [0]); $ I = strlen ($ match [0])-1; $ realnum; if ($ magic) {$ blen; $ ctype = 1 ;}} else {if (ord ($ cur) >=128) {$ cstep = 2; $ length = 2; $ I = 1; $ realnum; if ($ magic) {$ blen; $ ctype = 1 ;}} else {$ cstep = 1; $ length = 1; $ realnum; if ($ magic) {$ alen ;}} if ($ magic) {if ($ blen * 2 $ alen) = ($ len * 2) break; if ($ blen * 2 $ alen) = ($ len * 2 1) {if ($ ctype = 1) {$ length-= $ cstep; break;} else {break ;}} else {if ($ realnum ==$ len) break ;}} unset ($ cur ); unset ($ alen); unset ($ blen); unset ($ realnum); unset ($ ctype); unset ($ cstep); return substr ($ title, $ start, $ length );}
|