Get the App
SLTechnology News&Howtos  ›  Development  › 

How does php determine how many characters a string contains

Shulou Source: shulou.com Published: 2022-06-01 18:38:47 10月26日 Update

This article mainly explains "php how to judge how many characters a string contains". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how php determines how many characters a string contains.

Php method to judge how many characters a string contains: 1, use the strlen () function, syntax "strlen ($string)"; 2, use mb_strlen () function, syntax "mb_strlen ($string, 'character encoding')".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php determines how many characters the string contains

In php, to judge how many characters a string contains is to determine the length of the string.

There are two functions commonly used in PHP to calculate the length of a string, which are the strlen () and mb_strlen () functions. When dealing with all-English strings, the two functions have the same effect, while when dealing with mixed or pure Chinese strings, the two functions will have some differences.

1. Strlen () function

The strlen () function returns the length of a given string in the following syntax format:

Strlen ($string)

The parameter $string is the string whose length needs to be calculated. Returns 0 if $string is empty.

Note: in the strlen () function, numbers, English, decimal points, underscores and spaces account for one character length, while one GB2312-encoded Chinese character occupies two character length and one UTF-8-encoded Chinese character takes up three character length.

Example:

2. Mb_strlen () function

The mb_strlen () function can also return the length of a string in the following syntax format:

Mb_strlen ($str [, $encoding = mb_internal_encoding ()])

Where $str is the string whose length needs to be calculated, and $encoding is an optional parameter for character encoding, or internal character encoding if omitted.

The return value of the mb_strlen () function is the number of characters contained in the string $str with $encoding encoding, or false if $encoding is invalid.

Note: unlike the strlen () function, in the mb_strlen () function, both Chinese characters, English, numbers, decimal points, underscores and spaces occupy only one character length.

Example:

At this point, I believe you have a deeper understanding of "how php determines how many characters a string contains". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Character string function length code two grammar Chinese Chinese English underscore hello content parameter decimal decimal number method format example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Shulou Technology Microsoft NVidia