PHP count_chars()函数讲解

作者:袖梨 2026-05-31
今天小编就为大家分享一篇关于PHP count_chars()函数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

PHP count_chars() 函数

实例

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

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

定义和用法

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

语法

count_chars( _string,mode_ )

img_64cc7fc899c3630.png?20191141610

img_64cc7fc8a738831.png?201911416134

实例 1

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

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

实例 2

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

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

实例 3

统计 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>"; } ?>

总结

相关文章

精彩推荐