PHP Conference Japan 2024

count_chars

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

count_chars返回字串中使用的字元資訊

說明

count_chars(字串 $string, 整數 $mode = 0): 陣列|字串

計算 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
"字串中有 $val 個 \"" , chr($i) , "\"。\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 則筆記

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 )
?>
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';
}
}

?>
seb at synchrocide dot net
20 年前
在經過多次嘗試錯誤,試圖建立一個函式來找出字串中不重複字元的數量後,我找到了 count_chars() 函式 - 我 20 多行的無用程式碼被它取代了

<?
函式 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

?>

我不知道這可以用在哪裡,但它蠻有趣的
匿名
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: // same as 0 but only byte-values with a frequency greater than zero are listed.
foreach ($result as $key => $value) {
if (
$value == 0) {
unset(
$result[$key]);
}
}
break;
case
2: // same as 0 but only byte-values with a frequency equal to zero are listed.
foreach ($result as $key => $value) {
if (
$value > 0) {
unset(
$result[$key]);
}
}
break;
case
3: // a string containing all unique characters is returned.
$buildString = '';
foreach (
$result as $key => $value) {
if (
$value > 0) {
$buildString .= chr($key);
}
}
return
$buildString;
case
4: // a string containing all not used characters is returned.
$buildString = '';
foreach (
$result as $key => $value) {
if (
$value == 0) {
$buildString .= chr($key);
}
}
return
$buildString;
}

// change key names...
foreach ($result as $key => $value) {
$result[chr($key)] = $value;
unset(
$result[$key]);
}

return
$result;

}
?>
Andrey G
4 年前
檢查兩個字串是否為異位構詞 (Anagram)

<?php

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

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

?>
pzb at novell dot com
17 年前
這個函式很適合用於輸入驗證。我經常需要檢查字串中的所有字元是否都是 7 位元 ASCII 字元(且不為 null)。這是我目前找到最快的函式

<?php
function is7bit($string) {
// 空字串視為 7 位元乾淨
if (!strlen($string)) {
return
true;
}
// count_chars 會以遞增的八位元組順序返回字元
$str = count_chars($str, 3);
// 檢查是否有 null 字元
if (!ord($str[0])) {
return
false;
}
// 檢查是否有 8 位元字元
if (ord($str[strlen($str)-1]) & 128) {
return
false;
}
return
true;
}
?>
phpC2007
17 年前
這是一個用來計算字串中不同字元數量個數的函式。它可以作為一個支援 utf8 的簡化版 count_chars(但僅限於單一模式)...

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

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

return
$num;
}
?>
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