Home  >  Article  >  Backend Development  >  Introduction to the usage of PHP’s count_chars() function

Introduction to the usage of PHP’s count_chars() function

WBOY
WBOYOriginal
2023-06-27 17:15:411394browse

In PHP programming, string operations are very common. Sometimes we need to count the number of occurrences of each character in a string. In this case, we can use PHP's count_chars() function. This article will introduce in detail how to use PHP's count_chars() function.

1. Overview of count_chars() function

The count_chars() function is a built-in function in PHP, used to count the number of occurrences of each character in a string.

bool count_chars ( string $string [, int $mode = 0 ] )

Parameter description:

$string: A string in which the number of occurrences of characters needs to be counted.

$mode: Optional parameter, specify the return form:

  • 0 (or default value): Returns a one-dimensional array, counting the occurrences of all ASCII codes.
  • 1: Returns a list of characters.
  • 2: Returns a list of all occurrences of non-repeating characters.

2. Use the count_chars() function to count the number of times a character appears

  1. Count the number of times each character appears

The following is a simple example of.

3cf6f371f35b5a52b0ddc24c2b617a07

The output result is as follows:

Array
(

[32] => 1
[33] => 1
[72] => 1
[101] => 1
[108] => 3
[111] => 2
[114] => 1
[119] => 1
[100] => 1

)

As you can see from the result, the count_chars() function returns An array is created, the keys of the array are ASCII code values, and the values ​​are the number of times a character appears. For example, the ASCII code value of the character "H" is 72, and it appears once in the string.

  1. Count the character list where non-repeating characters appear

The following example demonstrates how to return a list of all non-repeating characters in a string.

98432e92fa1cd2de6f7a66603565fc32

The output result is as follows:

H
e
l
o
W
r
d
!

As you can see from the results, the count_chars() function returns a string containing all non-repeating characters in the string. Moreover, we used the str_split() function to convert the string into a character array and output it one by one in the foreach loop.

3. Summary

This article introduces in detail the usage of the count_chars() function in PHP, including counting the number of occurrences of each character and returning a list of non-repeating characters. We can use this function to quickly count the characters in the string, which brings great convenience to our programming.

The above is the detailed content of Introduction to the usage of PHP’s count_chars() function. 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