php自带count 函数,PHP4新增函数count_chars简介

本文翻译自www.php.net。

本函数适用于PHP4.0b4以上版本。

函数名称:count_chars -- 返回字符串中的字符使用信息。

简介:

mixed count_chars (string string [, mode])

统计[0..255]字符在字符串中出现的频率,并以多种方式返回结果。默认模式为模式0,下面是各种模式的说明:

模式0:以数组方式返回字符出现频率

模式1:和模式0相同,但只列出频率大于零的结果

模式2:和模式0相同,但只列出频率等于零的结果

模式3:返回该字符串中所有使用的字符的出现频率

模式4:返回一个字符串,这个字符串包含所有未出现的字符

例子:

shaun@phpboards.com

08-Sep-2000 02:09

Usage Example

Using count_chars() to figure out which characters are in a string, and how many instances of each character were present:

$data = "Two Ts and one F.";

$result = (count_chars($data, 0));

for ($i=0; $i 

if ($result[$i])

echo "There were $result[$i] instances of "" . (chr($i)) ."" in the string.

";

}

}

?>

The results would be:

There were 4 instances of " " in the string.

There were 1 instances of "." in the string.

There were 1 instances of "F" in the string.

There were 2 instances of "T" in the string.

There were 1 instances of "a" in the string.

There were 1 instances of "d" in the string.

There were 1 instances of "e" in the string.

There were 2 instances of "n" in the string.

There were 2 instances of "o" in the string.

There were 1 instances of "s" in the string.

There were 1 instances of "w" in the string.

Application Example

Using count_chars() to perform one check on the contents of a potential password string: if any character appears more than 5 times in the string, reject the password as unsafe.

# $password is passed to the script on runtime

$result = (count_chars($password, 0));

for ($i=0; $i 

if ($result[$i] > 5)

die("Your password contained too many instances of the same

character. Please go back and choose a more random password.");

}

?>

Hope these help!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值