Home >Backend Development >PHP Tutorial >PHP function count_chars() that returns all information of a string

PHP function count_chars() that returns all information of a string

黄舟
黄舟Original
2017-11-02 10:20:101717browse

Example

Returns a string containing all the different characters used in "Hello World!" (mode 3):

<?php
$str = "Hello World!";
echo count_chars($str,3);
?>

Definition and usage

count_chars() FunctionReturns information about the characters used in a string (for example, the number of times an ASCII character appears in a string, or whether a character has already been used in a string).

Syntax

count_chars(string,mode)

Parameters                                                                                                                                                                                                                            . Specifies the string to check. ​

mode ​ ​ ​ ​ ​ Optional. Specifies the return mode. The default is 0. There are different return patterns: , the number of occurrences is the key value, only the values ​​with the number of occurrences 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 ​​with the number of occurrences equal to 0 are listed

3 -String, with all different characters that have been used 4 -String, with all unused different characters

Technical details

Return value: Depends on the specified mode parameter.

PHP Version: 4+

More examples

Example 1

Returns a string containing all unused words in "Hello World!" Passed characters (mode 4):

<?php
$str = "Hello World!";
echo count_chars($str,4);
?>

Example 2

在本实例中,我们将使用 count_chars() 来检查字符串,返回模式设置为 1。模式 1 将返回一个数组,ASCII 值为键名,出现的次数为键值:

<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>

实例 3

统计 ASCII 字符在字符串中出现的次数另一个实例:

<?php
$str = "PHP is pretty fun!!";
$strArray = count_chars($str,1);

foreach ($strArray as $key=>$value)
{
echo "The character <b>&#39;".chr($key)."&#39;</b> was found $value time(s)<br>";
}
?>

count_chars实例

<?php
$data  =  "Two Ts and one F." ;
foreach ( count_chars ( $data ,  1 ) as  $i  =>  $val ) {   
echo  "There were  $val  instance(s) of \""  ,  chr ( $i ) ,  "\" in the string.<br/>" ;
}
?>

运行结果:

There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.

The above is the detailed content of PHP function count_chars() that returns all information of a string. 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