PHP Conference Japan 2024

count_chars

(PHP 4, PHP 5, PHP 7, PHP 8)

count_chars返回有关字符串中使用的字符的信息

描述

count_chars(string $string, int $mode = 0): array|string

计算string中每个字节值(0..255)出现的次数,并以各种方式返回。

参数

string

要检查的字符串。

mode

参见返回值。

返回值

根据modecount_chars() 返回以下内容之一

  • 0 - 一个数组,其中字节值为键,每个字节的频率为值。
  • 1 - 与 0 相同,但仅列出频率大于零的字节值。
  • 2 - 与 0 相同,但仅列出频率等于零的字节值。
  • 3 - 返回一个包含所有唯一字符的字符串。
  • 4 - 返回一个包含所有未用字符的字符串。

变更日志

版本 描述
8.0.0 在此版本之前,如果函数失败,则返回false

示例

示例 #1 count_chars() 示例

<?php
$data
= "Two Ts and one F.";

foreach (
count_chars($data, 1) as $i => $val) {
echo
"There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
?>

以上示例将输出

There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.

参见

  • strpos() - 在字符串中查找子字符串第一次出现的字符位置
  • substr_count() - 统计子字符串出现的次数

添加注释

用户贡献的注释 8 条注释

24
marcus33cz
12 年前
如果您在使用多字节字符串时使用 count_chars 遇到问题,您可以更改页面编码。或者,您也可以使用此函数的 mb_count_chars 版本。基本上它是原始函数的“1”模式。

<?php
/**
* 统计多字节字符串中字符出现的次数
* @param string $input UTF-8 数据
* @return array 字符的关联数组。
*/
function mb_count_chars($input) {
$l = mb_strlen($input, 'UTF-8');
$unique = array();
for(
$i = 0; $i < $l; $i++) {
$char = mb_substr($input, $i, 1, 'UTF-8');
if(!
array_key_exists($char, $unique))
$unique[$char] = 0;
$unique[$char]++;
}
return
$unique;
}

$input = "Let's try some Greek letters: αααααΕεΙιΜμΨψ, Russian: ЙЙЫЫЩН, Czech: ěščřžýáíé";
print_r( mb_count_chars($input) );
//返回:Array ( [L] => 1 [e] => 7 [t] => 4 ['] => 1 [s] => 5 [ ] => 9 [r] => 3 [y] => 1 [o] => 1 [m] => 1 [G] => 1 [k] => 1 [l] => 1 [:] => 3 [α] => 5 [Ε] => 1 [ε] => 1 [Ι] => 1 [ι] => 1 [Μ] => 1 [μ] => 1 [Ψ] => 1 [ψ] => 1 [,] => 2 [R] => 1 [u] => 1 [i] => 1 [a] => 1 [n] => 1 [Й] => 2 [Ы] => 2 [Щ] => 1 [Н] => 1 [C] => 1 [z] => 1 [c] => 1 [h] => 1 [ě] => 1 [š] => 1 [č] => 1 [ř] => 1 [ž] => 1 [ý] => 1 [á] => 1 [í] => 1 [é] => 1 )
?>
2
Eric Pecoraro
19 年前
<?php

// 在字符串中要求 (n) 个唯一字符
// 下面函数的修改,增加了在给定字符串中需要多少个唯一字符的灵活性。

$pass = '123456' ; // true
$pass = '111222' ; // false

req_unique($pass,3);

function
req_unique($string,$unique=3) {
if (
count(count_chars($string,1)) < $unique) {
echo
'false';
}else{
echo
'true';
}
}

?>
1
seb at synchrocide dot net
20 年前
经过多次尝试和错误,试图创建一个查找字符串中唯一字符数量的函数,我偶然发现了 count_chars() - 我的 20 多行无用的代码被此取代了

<?
function unichar($string) {
$two= strtolower(str_replace(' ', '', $string));
$res = count(count_chars($two, 1));
return $res;
}

/* 示例 :: */

echo unichar("bob"); // 2
echo unichar("Invisibility"); //8
echo unichar("The quick brown fox slyly jumped over the lazy dog"); //26

?>

我不知道这可以在哪里使用,但它很有趣
0
Anonymous
8 年前
count_chars 支持多字节。

<?php

function mb_count_chars ($string, $mode = 0) {

$result = array_fill(0, 256, 0);

for (
$i = 0, $size = mb_strlen($string); $i < $size; $i++) {
$char = mb_substr($string, $i, 1);
if (
strlen($char) > 1) {
continue;
}

$code = ord($char);
if (
$code >= 0 && $code <= 255) {
$result[$code]++;
}
}

switch (
$mode) {
case
1: // 与 0 相同,但仅列出频率大于零的字节值。
foreach ($result as $key => $value) {
if (
$value == 0) {
unset(
$result[$key]);
}
}
break;
case
2: // 与 0 相同,但仅列出频率等于零的字节值。
foreach ($result as $key => $value) {
if (
$value > 0) {
unset(
$result[$key]);
}
}
break;
case
3: // 返回包含所有唯一字符的字符串。
$buildString = '';
foreach (
$result as $key => $value) {
if (
$value > 0) {
$buildString .= chr($key);
}
}
return
$buildString;
case
4: // 返回包含所有未使用字符的字符串。
$buildString = '';
foreach (
$result as $key => $value) {
if (
$value == 0) {
$buildString .= chr($key);
}
}
return
$buildString;
}

// 更改键名...
foreach ($result as $key => $value) {
$result[chr($key)] = $value;
unset(
$result[$key]);
}

return
$result;

}
?>
-1
Andrey G
4 年前
检查两个字符串是否为字谜

<?php

function isAnagram($string1, $string2)
{
return
count_chars($string1, 1) === count_chars($string2, 1);
}

isAnagram('act', 'cat'); // true

?>
-1
pzb at novell dot com
17 年前
此函数非常适合输入验证。我经常需要检查字符串中的所有字符是否为 7 位 ASCII(且不为空)。这是我迄今为止发现的最快函数

<?php
function is7bit($string) {
// 空字符串为 7 位干净
if (!strlen($string)) {
return
true;
}
// count_chars 按升序八位字节顺序返回字符
$str = count_chars($str, 3);
// 检查空字符
if (!ord($str[0])) {
return
false;
}
// 检查 8 位字符
if (ord($str[strlen($str)-1]) & 128) {
return
false;
}
return
true;
}
?>
-3
phpC2007
17 年前
这是一个用于统计字符串中字符串数量的函数。它可以用作简单的支持 UTF8 的 count_chars(但仅限于单个模式)...

<?php
function utf8_count_strings($stringChar)
{
$num = -1;
$lenStringChar = strlen($stringChar);

for (
$lastPosition = 0;
$lastPosition !== false;
$lastPosition = strpos($textSnippet, $stringChar, $lastPosition + $lenStringChar))
{
$num++;
}

return
$num;
}
?>
-5
mlong at mlong dot org
22 年前
// 两个函数的用途

<?php
$string
="aaabbc";

// 您只想统计字母 a 的数量
$acount=substr_count($string,"a");

// 您想统计字母 a 和字母 b 的数量
$counts=count_chars($string,0);
$acount=$counts[ord("a")];
$bcount=$counts[ord("b")];
?>
To Top