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

一个新手
Release: 2023-03-15 19:08:02
Original
2802 people have browsed it

Get the string length using the strlen() function.

The syntax is as follows:

int strlen(string str)
Copy after login

Test:

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

The results are as follows

10

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

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");
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template