This article mainly introduces PHP's methods for determining and intercepting the length of a mix of Chinese and English strings, and analyzes php's skills in traversing, conversion, intercepting, and computing Chinese and English strings in combination with examples, for more information about how to determine and intercept the length of a mix of Chinese and English strings, combined with examples, this article analyzes php's operation skills related to Chinese and English string traversal, conversion, truncation, and computing. For more information, see
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 ;}
The above is a detailed explanation of PHP's example of determining and intercepting the length of a mix of Chinese and English strings. For more information, see other related articles in the first PHP community!