PHP count_chars()函数

PHP count_chars()函数

PHP count_chars()是最重要的字符串函数。它用于返回字符串中字符的信息。

语法

count_chars(string,mode);
参数 描述 必需/可选
string 要检查的字符串 必需
mode 指定返回模式 可选

注意:根据’mode’参数的不同,’count_chars()’函数返回以下之一:

  • 0:返回一个以字节值为键,每个字节频率为值的数组。
  • 1:与0相同,但只列出频率大于零的字节值。
  • 2:与0相同,但只列出频率等于零的字节值。
  • 3:返回一个包含所有唯一字符的字符串。
  • 4:返回一个包含所有未使用字符的字符串。

示例1

<?php
str = "Hello World!";
echo "Your given string: ".str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,3);
?>

输出:

Your given string: Hello World!
By using 'count_chars()' function your string is : !HWdelor

示例2

<?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.\n";
}
?>

输出:

Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] => 3 [111] => 2 [114] => 1 )

示例3

<?php
str = "PHP is Lovely Language!";strArray = count_chars(str,1);

foreach (strArray as key=>value)
   {
   echo "The character <b>'".chr(key)."'</b> was foundvalue time(s)<br>";
   }
?>

输出:

The character ' ' was found 3 time(s)
The character '!' was found 1 time(s)
The character 'H' was found 1 time(s)
The character 'L' was found 2 time(s)
The character 'P' was found 2 time(s)
The character 'a' was found 2 time(s)
The character 'e' was found 2 time(s)
The character 'g' was found 2 time(s)
The character 'i' was found 1 time(s)
The character 'l' was found 1 time(s)
The character 'n' was found 1 time(s)
The character 'o' was found 1 time(s)
The character 's' was found 1 time(s)
The character 'u' was found 1 time(s)
The character 'v' was found 1 time(s)
The character 'y' was found 1 time(s)

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程