Home  >  Article  >  Backend Development  >  How to count several characters in php

How to count several characters in php

藏色散人
藏色散人Original
2021-12-09 09:30:063110browse

How to calculate several characters in php: 1. Get the length of the given string string through the strlen function; 2. Get the number of characters in the string using the mb_strlen function.

How to count several characters in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to calculate how many characters in php?

PHP Character Count Calculation

Sometimes, for example, when registering a user, you need to check the length of the user name. The following two functions may be used to make the judgment

  • strlen

int strlen ( string $string )

Returns the length of the given string string.

Note: It returns the number of bytes in the string, not the number of characters. Assuming a Chinese character "you" may return 3, it depends on what encoding you use

  • mb_strlen

int mb_strlen ( string $str [, string $encoding ] )

Returns the number of characters in the string.

str

Specific string

encoding

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

Note: Be sure to open php_mbstring.dll in the php.ini file.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to count several characters 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