Php uses strlen () to determine the length of a Chinese character string

Source: Internet
Author: User
The strlen () function can be used to determine the character length in a simple fuzzy way. for example, if you use different Chinese characters and pinyin or encoding, the strlen () function is used to calculate different values, next I will introduce how to use strlen () functions. we passed... the strlen () function can be used to determine the character length in a simple fuzzy way. for example, if you use different Chinese characters and pinyin or encoding, the strlen () function is used to calculate different values, next I will introduce how to use strlen () functions.

We often use the strlen () function in PHP to understand the length of a string. the usage is as follows.

PHP strlen () function definition and usage

The strlen () function returns the length of the string.

Syntax: Strlen (string)

Parameters: String

Description: Required, specifying the string to be checked.

The php instance code is as follows:

 

// Output: 12

In this case, PHP built-in functions such as strlen () and mb_strlen () are used to count the length of a string by calculating the number of bytes occupied by the string. an English character occupies 1 byte. for example:

$ EnStr = 'Hello, China! '; Echo strlen ($ enStr); // output: 12

Chinese is not the case. Chinese websites generally use two types of encoding: gbk, gb2312, or UTF-8. UTF-8 is compatible with more characters, so it is favored by many webmasters, gbk and UTF-8 are encoded differently for Chinese characters, resulting in differences in the number of bytes occupied by Chinese characters in gbk and UTF-8 encoding.

Each Chinese character occupies 2 bytes in gbk encoding. the code is as follows:

$ ZhStr = 'Hello, China! '; Echo strlen ($ zhStr); // output: 12

Each Chinese character occupies 3 bytes in UTF-8 encoding. the code is as follows:

$ ZhStr = 'Hello, China! '; Echo strlen ($ zhStr); // output: 18

So how can we calculate the length of this set of Chinese strings? Some people may say that the length of a Chinese string obtained in gbk is divided by 2. is it okay to divide it by 3 in UTF-8 encoding? However, you need to consider that the string is not honest, and 99% of the cases will appear in a mix of Chinese and English.

This is a piece of code in WordPress. The main idea is to break down the string into individual units using regular expressions, and then calculate the number of units, that is, the length of the string. the code is as follows: only UTF-8 encoded strings can be processed:

$ ZhStr = 'Hello, China! '; $ Str = 'Hello, China! '; // Calculate the length of a Chinese string function utf8_strlen ($ string = null) {// splits the string into the unit preg_match_all ("/. /us ", $ string, $ match); // return the number of units returned return count ($ match [0]);} echo utf8_strlen ($ zhStr); // output: 6 echo utf8_strlen ($ str); // output: 9

The following code is used to calculate the differences between the number of characters and the number of bytes:

/Assume that the current page is encoded as GBK

  ";//
Line Feed
// Assume that the current page is encoded as a UTF-8
  

Iconv_strlen can calculate the exact number of characters regardless of the encoding.

// Assume that the current page is encoded as GBK

   

// Assume that the current page is encoded as a UTF-8

    

Iconv_strlen can calculate the exact number of characters regardless of the encoding.


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.