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

黄舟
Release: 2023-03-16 21:52:01
Original
1725 people have browsed it

Example

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

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

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

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);
?>
Copy after login

Example 2

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

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

实例 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>";
}
?>
Copy after login

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/>" ;
}
?>
Copy after login

运行结果:

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

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!

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