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

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 count_chars() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 635 ::
收藏到网摘: 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

登陆 | 还没注册?