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
<?
$ Str = "abcdefg People's Liberation Army of the People's Republic of China, Communist Party of China, Chinese people ";
$ Num = strlen ($ str); // $ num string length.
Echo $ num. "<br> ";
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

<? Php

$ TestStr = 'if I didn't tell you, you should never mess with guess. This is not good! ';
$ TestStr = eregi_replace ("[[: alnum:] | [[: punct:] | [[: space:]", '', $ testStr );
Echo ($ testStr );
Echo ('all Chinese characters in the string are: '. mb_strlen ($ testStr ));
?>


# 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 exist
If ($ hasCC &&! $ HasAsc) # only Chinese characters
Return strlen ($ str)/2;
If (! $ HasCC & $ hasAsc) # only Ascii characters
Return 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 odd
Return substr ($ str, 0, $ len + $ len-2 );
Else
Return 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 );
Else
Return substr ($ str, 0, $ len-1 );
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

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

Learn more >

Alibaba Cloud Free Trial

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

Learn more >

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.