ホームページ  >  記事  >  バックエンド開発  >  文字列のすべての情報を返す PHP 関数 count_chars()

文字列のすべての情報を返す PHP 関数 count_chars()

黄舟
黄舟オリジナル
2017-11-02 10:20:101633ブラウズ

「Hello World!」(モード 3) で使用されるすべての異なる文字を含む文字列を返します:

<?php
$str = "Hello World!";
echo count_chars($str,3);
?>

定義と使用法

count_chars() 関数文字列で使用される文字数を返します。 (たとえば、文字列内に ASCII 文字が出現する回数、または文字列内で文字がすでに使用されているかどうか)。

syntax

count_chars(string,mode)

parameters構文チェックする文字列を指定します。

モード オプション。復帰モードを指定します。デフォルトは 0 です。次のようなさまざまな戻りモードがあります:

配列 値はキーの名前、出現回数のみ発生回数がリストされます。回数の値が 0 -2 を超えています。配列、ASCII 値です。はキー名、回数はキーの値、回数の値のみが 0

3 の文字列で、すべて異なる文字が使用されます

。带 4 - 未使用の異なる文字をすべて含む文字列

技術的な詳細

戻り値: 指定された Mode パラメータによって異なります。

PHP バージョン: 4+

その他の例

例 1

「Hello World!」で使用されていないすべての文字を含む文字列を返す (モード 4):

<?php
$str = "Hello World!";
echo count_chars($str,4);
?>

例 2

在本实例中,我们将使用 count_chars() 来检查字符串,返回模式设置为 1。模式 1 将返回一个数组,ASCII 值为键名,出现的次数为键值:

<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>

实例 3

统计 ASCII 字符在字符串中出现的次数另一个实例:

<?php
$str = "PHP is pretty fun!!";
$strArray = count_chars($str,1);

foreach ($strArray as $key=>$value)
{
echo "The character <b>&#39;".chr($key)."&#39;</b> was found $value time(s)<br>";
}
?>

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.<br/>" ;
}
?>

运行结果:

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.

以上が文字列のすべての情報を返す PHP 関数 count_chars()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。