PHP count_chars()函數
PHP count_chars()是最重要的字符串函數。愛掏網 - it200.com它用于返回字符串中字符的信息。愛掏網 - it200.com
count_chars(string,mode);
參數 | 描述 | 必需/可選 |
---|---|---|
string | 要檢查的字符串 | 必需 |
mode | 指定返回模式 | 可選 |
注意:根據’mode’參數的不同,’count_chars()’函數返回以下之一:
- 0:返回一個以字節值為鍵,每個字節頻率為值的數組。愛掏網 - it200.com
- 1:與0相同,但只列出頻率大于零的字節值。愛掏網 - it200.com
- 2:與0相同,但只列出頻率等于零的字節值。愛掏網 - it200.com
- 3:返回一個包含所有唯一字符的字符串。愛掏網 - it200.com
- 4:返回一個包含所有未使用字符的字符串。愛掏網 - it200.com
示例1
<?php
str = "Hello World!";
echo "Your given string: ".str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,3);
?>
輸出:
Your given string: Hello World!
By using 'count_chars()' function your string is : !HWdelor
示例2
<?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";
}
?>
輸出:
Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] => 3 [111] => 2 [114] => 1 )
示例3
<?php
str = "PHP is Lovely Language!";strArray = count_chars(str,1);
foreach (strArray as key=>value)
{
echo "The character <b>'".chr(key)."'</b> was foundvalue time(s)<br>";
}
?>
輸出:
The character ' ' was found 3 time(s)
The character '!' was found 1 time(s)
The character 'H' was found 1 time(s)
The character 'L' was found 2 time(s)
The character 'P' was found 2 time(s)
The character 'a' was found 2 time(s)
The character 'e' was found 2 time(s)
The character 'g' was found 2 time(s)
The character 'i' was found 1 time(s)
The character 'l' was found 1 time(s)
The character 'n' was found 1 time(s)
The character 'o' was found 1 time(s)
The character 's' was found 1 time(s)
The character 'u' was found 1 time(s)
The character 'v' was found 1 time(s)
The character 'y' was found 1 time(s)
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。