PHP count_chars() 函数

2024 年 9 月 4 日 | 阅读 2 分钟

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

语法

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

注意:根据模式,count_chars() 函数将返回以下之一

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

示例 1

输出

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

示例 2

输出

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

示例 3

输出

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)

下一主题PHP 字符串