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

WBOY
Release: 2023-06-27 20:12:01
Original
1415 people have browsed it

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.

$str = "Hello World!";
$char_count = count_chars($str, 1);

print_r($char_count);
?>

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

)

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.

$str = "Hello World!";
$char_list = count_chars($str, 2);

foreach(str_split($char_list) as $char) {

echo $char . PHP_EOL;
Copy after login

}
?>

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!

Related labels:
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