PHP uses an example of how to determine and intercept the length of a mix of Chinese and English strings,
This example describes how PHP judges and intercepts the length of a mix of Chinese and English strings. We will share this with you for your reference. The details are as follows:
/*** Determine the length of a Chinese/English string * @ param unknown_type $ str * @ param unknown_type $ charset */function strLength ($ str, $ charset = 'utf-8 ') {if ($ charset = 'utf-8') $ str = iconv ('utf-8', 'gb2312 ', $ str ); $ num = strlen ($ str); $ cnNum = 0; for ($ I = 0; $ I <$ num; $ I ++) {if (ord (substr ($ str, $ I + 1, 1)> 127) {$ cnNum ++; $ I ++ ;}} $ enNum = $ num-($ cnNum * 2); $ number = ($ enNum/2) + $ cnNum; return ceil ($ number );}
/*** Intercept string with a mix of Chinese and English characters * @ param unknown_type $ sourcestr * @ param unknown_type $ cutlength */function cut_str ($ sourcestr, $ cutlength) {$ returnstr = ''; $ I = 0; $ n = 0; $ str_length = strlen ($ sourcestr); // string bytes while ($ n <$ cutlength) and ($ I <= $ str_length) {$ temp_str = substr ($ sourcestr, $ I, 1); $ ascnum = Ord ($ temp_str ); // obtain the ascii code of the $ I character in the string if ($ ascnum> = 224) // if the ASCII bit height is 224, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 3); // count three consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 3; // The actual bytes are calculated as 3 $ n ++; // String Length sensor 1} elseif ($ ascnum> = 192) // If the ASCII bit height is 192, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 2); // count 2 consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 2; // The actual Byte is calculated as 2 $ n ++; // string length gauge 1} elseif ($ ascnum> = 65 & $ ascnum <= 90) // if it is a capital letter, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // the actual number of bytes is still 1 $ n ++; // but consider the overall appearance. uppercase letters are counted as a high character} else // in other cases, including lowercase letters and halfwidth punctuation marks, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // the actual number of bytes is 1 $ n = $ n + 0.5; // lower-case letters, halfwidth punctuation marks, and half-width upper-limit characters ...}} if ($ str_length> $ cutlength) {$ returnstr = $ returnstr. "... "; // when the length is exceeded, add a ellipsis at the end} return $ returnstr ;}
PS: here we recommend several online character statistics tools for your reference:
Online word count statistics tools:
Http://tools.jb51.net/code/zishutongji
Online character statistics and editing tools:
Http://tools.jb51.net/code/char_tongji