Php Chinese string statistics and Interception

Source: Internet
Author: User
When reviewing the basics over the past few days, I can see the character string chapter. I have a question for paging the article! If so, write it by yourself. I thought it was very simple, but I had to go over the results for two days. During this period, I always watched the East and the West. I didn't focus on criticizing myself at all. I finally got a new one. Only the user needs to pass in the current character encoding o (character □character) o

When reviewing the basics over the past few days, I can see the character string chapter. I have a question for paging the article! If so, write it by yourself. I thought it was very simple, but I had to wait for two days. During this period, I always watched the East and the West. I didn't focus on it at all. // I criticized myself and finally made a new one. Only the user needs to pass in the current character encoding o (character □character) o

When reviewing the basics over the past few days, I can see the character string chapter. I have a question for paging the article!

If so, write it by yourself. I thought it was very simple, but I had to wait for two days. During this period, I always watched the East and the West. I didn't focus on it at all ............ // Criticize yourself

Fortunately, I finally got one. However, the user must pass in the current character encoding o (encoding □encoding) o. All found on the Internet are the use of hexadecimal intervals in the ASCII code of each character encoding to determine Chinese characters ............ // I admit that my technology is not good.

Below is the code. // Post a blog after writing it, to consolidate and deepen your impression:-D

Note: No matter how many bytes each English letter, Chinese character, or special character occupies, I always process it by one character.

1
 '; 4/** 5 * string statistics. Each character is calculated by one length. 6 * GBK is supported. UTF8 7 * is similar to mb_strlen () 8 * @ author tan Ningning 9 * @ time 2012-08-05 10 */11 if (! Function_exists ('strcount') 12 {13 function strcount ($ string, $ char = 'utf8') 14 {15 $ count = strlen ($ string ); 16 $ I = 0; // the current number of bytes is 17 $ j = 0; // Add 18 while by character ($ I <$ count) 19 {20 // english and halfwidth special characters 21 if (ord ($ string [$ I])> = 0 & ord ($ string [$ I]) <= 126) 22 {$ charset = 'en';} 23 // Chinese Character and fullwidth character 24 else 25 {$ charset = $ char;} 26 27 switch (strtolower ($ charset )) 28 {29 case 'gb2312': 30 case 'gbk': 31 $ I + = 1; 32 break; 33 case 'utf8': 34 $ I + = 2; 35 break; 36 case 'en': 37 default: 38 break; 39} 40 $ j ++; 41 $ I ++; 42} 43 return $ j; 44} 45} 46 else 47 {echo'

Fun strcount exists!

';} 48 49/** 50 * custom string truncation function to prevent mb_substr () 51 * is not enabled. The $ char entered by the user is used to determine the character set encoding of the current Chinese character. 52 * @ param int $ start starts with the number of characters 53 * @ param int $ offest offset, and the number of characters 54 * @ param str $ char output starting from $ start. The user manually enters the current Chinese character encoding 55 * @ author tan Ningning 56 * @ time 2012-08-05 57 */58 if (! Function_exists ('strsub') 59 {60 function strsub ($ string, $ start = 0, $ offest = 0, $ char = 'utf8 ') 61 {62 $ count = strlen ($ string); 63 $ rs = ''; 64 $ I = 0; // accumulated by the number of bytes 65 $ j = 0; // The total number of characters is 66 $ size = 1; // records the end position of each substr. For Chinese characters, you must consider the gbk and utf8 Conditions 67 while ($ I <$ count) 68 {69 // english and halfwidth special characters 70 if (ord ($ string [$ I])> = 0 & ord ($ string [$ I]) <= 126) 71 {$ charset = 'en';} 72 // Chinese Character and fullwidth character 73 else 74 {$ charset = $ char;} 75 76 switch (strtolower ($ charset )) 77 {78 case 'gb2312': 79 case 'gbk': 80 $ I + = 1; 81 $ size = 2; 82 break; 83 case 'utf8 ': 84 $ I + = 2; 85 $ size = 3; 86 break; 87 case 'en': 88 default: 89 $ size = 1; 90 break; 91} 92 93 if ($ j <intval ($ start + $ offest) & $ j >=$ start) 94 {95 $ tstart = intval ($ I-$ size) + 1; 96 $ rs. = substr ($ string, $ tstart, $ size); 97} 98 $ j ++; 99 $ I +++; 100} 101 return $ rs; 102} 103} 104 else105 {echo'

Fun strsub exists!

';} 106 107/* $ string = '2017 Chinese Character Hu Zong Wen Chinese Ah abcdefghijklmn · =-'; 123456789 echo 'substr (): ', substr ($ string, 9, 3 ),'
'; 109 echo' length: ', strcount ($ string ),'
'; 110 echo 'test truncation:', strsub ($ string, 0, 11 ),'
'; */111 112 $ fileContent = file_exists('reg.txt ')? File_get_contents('reg.txt '): ''; 113 114 $ count = strcount ($ fileContent); 115 $ page =! Isset ($ _ GET ['P'])? 1: $ _ GET ['P']; // obtain the current page number. The default value is 1116 $ pagesize = 350; // The number of characters on each page: 117 $ pagecount = $ count/$ pagesize; 118 $ pagecount = strpos ($ pagecount ,'. ')? Intval ($ pagecount) + 1: intval ($ pagecount); // total page number. If there is a decimal number, you need to add 1 page 119 $ start = $ page <= 1? 0: ($ page-1) * $ pagesize; 120 121 $ fileContent = strsub ($ fileContent, $ start, $ pagesize, 'utf8'); 122?> 123 124 125 131 132133

134 135

136 The last page "; 162} 163?> 164

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.