# Php statistics and calculation of Chinese characters word count code

Source: Internet
Author: User
Tags ereg ord strlen

Use ASCII code.

 The code is as follows: Copy code ";For (\$ I = 0; \$ I <\$ num; \$ I ++)If (ord (substr (\$ str, \$ I, 1)> 0xa0) \$ j ++;Echo \$ j/2; // \$ j/2 Chinese characters.?>

Remove all letters, numbers, punctuation marks, and spaces.

Code:

 The code is as follows: Copy code

# Calculate the length of a mix of Chinese and English strings

 The code is as follows: Copy code Function ccStrLen (\$ str){ \$ CcLen = 0;\$ AscLen = strlen (\$ str );\$ Ind = 0;\$ HasCC = ereg ("[xA1-xFE]", \$ str); # identify whether there are Chinese characters\$ HasAsc = ereg ("[x01-xA0]", \$ str); # identify whether ASCII characters existIf (\$ hasCC &&! \$ HasAsc) # only Chinese charactersReturn strlen (\$ str)/2;If (! \$ HasCC & \$ hasAsc) # only Ascii charactersReturn strlen (\$ str );For (\$ ind = 0; \$ ind <\$ ascLen; \$ ind ++){ If (ord (substr (\$ str, \$ ind, 1)> 0xa0){ \$ CcLen ++;\$ Ind ++;} Else{ \$ CcLen ++;} } Return \$ ccLen;} Function ccStrLeft (\$ str, \$ len) # extract Chinese and English strings from the left{ \$ AscLen = strlen (\$ str); if (\$ ascLen <= \$ len) return \$ str;\$ HasCC = ereg ("[xA1-xFE]", \$ str); # Same as above\$ HasAsc = ereg ("[x01-xA0]", \$ str );If (! \$ HasCC) return substr (\$ str, 0, \$ len );If (! \$ HasAsc)If (\$ len & 0x01) # if the length is oddReturn substr (\$ str, 0, \$ len + \$ len-2 );ElseReturn substr (\$ str, 0, \$ len + \$ len );\$ Cind = 0; \$ flag = 0;While (\$ cind <\$ ascLen){ If (ord (substr (\$ str, \$ cind, 1) <0xA1) \$ flag ++;\$ Cind ++;} If (\$ flag & 0x01)Return substr (\$ str, 0, \$ len );ElseReturn substr (\$ str, 0, \$ len-1 );}
Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD