Write a PHP program to check the number of Chinese characters in the string instance share _php instance

Source: Internet
Author: User

Sometimes we need to calculate the number of words contained in a string, and for the pure English string, the word number equals the length of the string,
The strlen function can be obtained, but what if the string contains Chinese? Mb_strlen can be achieved, but unfortunately not installed expansion, then realize it yourself.

PHP has an extension is generally required, we can use Mb_strlen to get the number of words in the string, used in general as follows:

$len = Mb_strlen ("You Are my little Apple", "utf-8");


Get string Length: 7.

What if the MB extension is not installed? Realize it yourself.

We must first understand the fact that: strings are made up of characters, and characters are represented by bytes, and each English character is a byte, corresponding to an ASCII code, and the ASCII code of the English character is less than 128, which is the hexadecimal 0x80. When a byte of ASCII code exceeds 127, That means the current byte is not a full character.

Like what

$STR = "You are my little Apple";

$STR{0} can take the first byte, let's look at what it is:

php > $STR = "You are my little Apple";
php > Echo $str {0};

is a garbled, it's just

Copy Code code as follows:
You

One of the bytes of the word, that is to say,
Copy Code code as follows:
You

This character is made up of more than one byte, so let's try this:

php > Echo $str {0}. $str {1}. $str {2};

You
As you can see, the three bytes are connected together to output, which becomes a complete

Copy Code code as follows:
You

So why are there three bytes instead of two or 4? This depends on the encoding of the string, I am here the console default is UTF8 encoded, in PHP, a UTF8 character is expressed in three bytes, if it is GBK encoding, it will be two bytes. As for the relationship between encoding and byte, this topic is relatively large, an article is not complete, please refer to this article: character encoding notes: Ascii,unicode and UTF8.

Knowing these, we can write a function of the word check ourselves, the approximate process is as follows:

1.for Loop Traversal byte 2. Determine if byte encoding is >= 0x80, yes, skip n bytes

I wrote a simple function to determine the length of a gbk or UTF8 string, for reference only:

<?php

function Mbstrlen ($str, $encoding = "UTF8")
{

  if ($len = strlen ($str)) = = 0) {return
    0;
  }

  $encoding = Strtolower ($encoding);

  if ($encoding = = "UTF8" or $encoding = = "Utf-8") {
    $step = 3;
  } elseif ($encoding = = "GBK" or $encoding = = gb2312 ") {
    $step = 2;
  } else {return
    false;
  }

  $count = 0;
  for ($i =0; $i < $len; $i + +) {
    $count + +;
    If the byte code is greater than 127, a few bytes of
    Ord ($str {$i}) >= 0x80) {
      $i = $i + $step -1;//minus 1 because the For loop itself is also $i++}<
    c21/>} return
  $count;
}

Echo Mbstrlen (Iconv ("Utf-8", "GBK", "You Are my Little Apple"), "GBK");
Echo Mbstrlen ("You Are my little Apple");

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.