当前位置:主页 > 软件编程 > PHP代码 >

PHP count_chars()函数讲解

时间:2021-04-11 08:55:14 | 栏目:PHP代码 | 点击:

PHP count_chars() 函数

实例

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

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

定义和用法

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

语法

count_chars( _string,mode_ )

实例 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>"; 
} 
?>

总结

您可能感兴趣的文章:

相关文章