count_chars() 返回字符串所用字符的信息

实例

返回一个字符串,包含所有在 "Hello World!" 中使用过的不同字符(模式 3):

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

输出
!HWdelor

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

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

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

输出
The character ' ' was found 3 time(s)
The character '!' was found 2 time(s)
The character 'H' was found 1 time(s)
The character 'P' was found 2 time(s)
The character 'e' was found 1 time(s)
The character 'f' was found 1 time(s)
The character 'i' was found 1 time(s)
The character 'n' was found 1 time(s)
The character 'p' was found 1 time(s)
The character 'r' was found 1 time(s)
The character 's' was found 1 time(s)
The character 't' was found 2 time(s)
The character 'u' was found 1 time(s)
The character 'y' was found 1 time(s)

 


定义和用法

count_chars() 函数返回字符串所用字符的信息(例如,ASCII 字符在字符串中出现的次数,或者某个字符是否已经在字符串中使用过)。


语法

count_chars(string,mode)

参数描述
string必需。规定要检查的字符串。
mode可选。规定返回模式。默认是 0。有以下不同的返回模式:
  • 0 - 数组,ASCII 值为键名,出现的次数为键值
  • 1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值
  • 2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值
  • 3 - 字符串,带有所有使用过的不同的字符
  • 4 - 字符串,带有所有未使用过的不同的字符
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值