Home >Backend Development >PHP Tutorial >How to use the count_chars function in php

How to use the count_chars function in php

藏色散人
藏色散人Original
2019-05-24 13:17:153456browse

Usage of count_chars function in php: [count_chars(string,mode)]. The count_chars function is used to return information about the characters used in a string, such as [count_chars($str,3)].

How to use the count_chars function in php

php The count_chars function returns a string that contains all the different characters used in the string. Its syntax is count_chars(string,mode), parameter string Required, specifies the string to be checked.

(Recommended tutorial: php video tutorial)

How to use the php count_chars function?

Function: Returns a string containing all different characters used in the string.

Syntax:

count_chars(string,mode)

Parameters:

string Required. Specifies the string to check.

mode Optional. Specifies the return mode. The default is 0.

The following are different return modes:

0 - Array, the ASCII value is the key name, the number of occurrences is the key value,

1 - Array, the ASCII value is the key name, the number of occurrences is the key value, only the values ​​whose occurrence number is greater than 0 are listed,

2 - Array, the ASCII value is the key name, the number of occurrences is the key value, only the values ​​are listed A value with an occurrence count equal to 0,

3 - a string with all used distinct characters,

4 - a string with all unused distinct characters .

Description: Return information about the characters used in the string

php count_chars() function usage example 1

<?php
//使用模式3输出
$str = "Hello php.cn!";
echo count_chars($str,3);
?>

Output:

!.Hcehlnop

php count_chars() function usage example 2

<?php
//使用模式1的数组形式输出
$str = "Hello php.cn!";
print_r(count_chars($str,1)) ;
?>

Output:

Array ( [32] => 1 [33] => 1 [46] => 1 [72] => 1 [99] => 1 [101] => 1 [104] => 1 [108] => 2 [110] => 1 [111] => 1 [112] => 2 )

The above is the detailed content of How to use the count_chars function 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