Home > Backend Development > PHP Problem > How to count several characters in php

How to count several characters in php

藏色散人
Release: 2023-03-14 11:24:02
Original
3157 people have browsed it

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

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

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!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template