当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > count_chars() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 count_chars() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 608 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The count_chars() function returns how many times an ASCII character occurs within a string and returns the information.
count_chars()函数的作用是:返回字符串所使用字符的信息的次数。

Syntax
语法

count_chars(string,mode)

Parameter参数 Description描述
string Required. The string to be checked
必要参数。指定需要被检查的字符串
mode Optional. Specifies the return modes. 0 is default. The different return modes are:
可选参数。指定需要返回的模式。具体如下(默认值是0):
  • 0 - an array with the ASCII value as key and number of occurrences as value
    0 –以ASCII值为键,以事件数为值的一个数组
  • 1 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences greater than zero
    1 -以ASCII值为键,以事件数为值的一个数组,仅列举大于0的事件
  • 2 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences equal to zero are listed
    2 -以ASCII值为键,以事件数为值的一个数组,仅列举等于0的事件
  • 3 - a string with all the different characters used
    3 –包含不同字符的字符串
  • 4 - a string with all the unused characters
    4 –包含未使用字符的字符串


Example 1
案例1

In this example we will use count_char() with mode 1 to check the string. Mode 1 will return an array with the ASCII value as key and how
many times it occurred as value (e.g. in the example below, the ASCII value for the letter "l" is 108, and it occurs three times):
在下面的案例中,我们将使用count_char()函数,选择mode 1来检查字符串。Mode 1将以ASCII值为键,以事件数为值的一个数组(如:在下面的案例中,"l"所对应的ASCII值是108,它出现了3次):

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

The output of the code above will be:
上述代码将输出下面的结果:

Array
(
[32] => 1
[33] => 1
[72] => 1
[87] => 1
[100] => 1
[101] => 1
[108] => 3
[111] => 2
[114] => 1
)


Example 2
案例2

In this example we will use the count_char() with mode 3 to check the string. Mode 3 will return a string with all the different characters used:
在下面的案例中,我们将使用count_char()函数,选择mode 3来检查字符串。Mode 3将返回一个包含不同字符的字符串:

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

The output of the code above will be:
上述代码将输出下面的结果:

 !HWdelor

评论 (0) All

登陆 | 还没注册?