PHP Gets the character length instance of the UTF8 string

Source: Internet
Author: User
Tags strlen

Tonight, when writing a form validation class for a framework, you need to determine whether a string length is within a specified range, and naturally, think of the strlen function in PHP.

The code is as follows

$str = ' Hello world! ';
echo strlen ($STR); Output 12

Test Chinese

The code is as follows

$str = ' Hello, world! ';
echo strlen ($STR); GBK or GB2312 lower output 12,utf-8 under output 18

PHP's built-in string length function strlen does not handle the Chinese string correctly, it gets just the number of bytes in the string. For GB2312 Chinese encoding, strlen gets twice times the number of Chinese characters, and for UTF-8 encoded Chinese, it is 3 times times the difference (in UTF-8 code, a Chinese character occupies 3 bytes).

The following example is from the famous WordPress, very accurate Oh, and also note that this function is only applicable to utf-8 encoded strings.

The code is as follows

function Utf8_strlen ($string =null) {
To decompose a string into cells
Preg_match_all ("/./us", $string, $match);
Return number of cells
return count ($match [0]);

But the above code does not handle gbk/gb2312 's Chinese string under the UTF-8 code, because the gbk/gb2312 character will be recognized as two characters and the number of words in the calculation will double, so I thought of such a way:

The code is as follows

$tmp = @iconv (' GBK ', ' utf-8 ', $str);
if (!empty ($tmp)) {
$str = $tmp;
Preg_match_all ('/./us ', $str, $match);
echo Count ($match [0]);

Compatible gbk/gb2312 and UTF-8 codes, passed with a small amount of data, but not yet determined to be completely correct

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: and provide relevant evidence. A staff member will contact you within 5 working days.