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

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP 中的 count_chars() 函数


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

登陆 | 还没注册?