Home >Backend Development >PHP Tutorial >How to get the length of a string in PHP

How to get the length of a string in PHP

一个新手
一个新手Original
2017-09-06 17:08:042739browse

Get the string length using the strlen() function.

The syntax is as follows:

int strlen(string str)

Test:

$str="123abc ,.;";
echo strlen($str);

The results are as follows

10

$str="107网站工作室";
echo strlen($str);

The results are as follows:

18

There will be questions at this time. There are 8 characters in total. Why is the displayed length 18? Because in the case of UTF-8 encoding, three characters are one Chinese character, 3+ 5*3=18;

Then the question is, can we display the number of characters in the string, such as 8 in this example

At this time, there is a simple way That is, you need to extend php.ini, remove the semicolon in front of extension=php_mbstring.dll, and then restart Apache

$str="107网站工作室";
echo mb_strlen($str,"UTF-8");

The operation will display the following results

8

mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )

encodingThe parameter is the character encoding. If omitted, the internal character encoding is used.


The above is the detailed content of How to get the length of a string in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn