加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php count_chars 返回字符串所用字符的信息

发布时间:2020-12-13 05:15:22 所属栏目:PHP教程 来源:网络整理
导读:count_chars 返回字符串所用字符的信息 count_chars函数基本语法 count_chars(string,mode) 统计 string 中每个字节值(0..255)出现的次数,使用多种模式返回结果。 count_chars参数介绍 参数 描述 string 必需。需要统计的字符串。 mode 可

count_chars — 返回字符串所用字符的信息

count_chars函数基本语法

count_chars(string,mode)

统计 string 中每个字节值(0..255)出现的次数,使用多种模式返回结果。

count_chars参数介绍

参数

描述

string

必需。需要统计的字符串。

mode

可选。规定返回模式。默认是 0。以下是不同的返回模式:

0 - 数组,ASCII 值为键名,出现的次数为键值

1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值

2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值

3 - 字符串,带有所有使用过的不同的字符

4 - 字符串,带有所有未使用过的不同的字符

count_chars返回值

根据不同的 mode, count_chars() 返回下列不同的结果:

0 - 以所有的每个字节值作为键名,出现次数作为值的数组。

1 - 与 0 相同,但只列出出现次数大于零的字节值。

2 - 与 0 相同,但只列出出现次数等于零的字节值。

3 - 返回由所有使用了的字节值组成的字符串。

4 - 返回由所有未使用的字节值组成的字符串。

count_chars实例

$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.
" ;

}

?>

在线运行

运行结果:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读