网络编程
位置:首页>> 网络编程>> php编程>> PHP count_chars()函数讲解

PHP count_chars()函数讲解

作者:php参考手册  发布时间:2023-06-05 09:17:25 

标签: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_ )

PHP count_chars()函数讲解

PHP count_chars()函数讲解

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

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com