PHP count_chars()函數(shù)
PHP count_chars()是最重要的字符串函數(shù)。愛掏網(wǎng) - it200.com它用于返回字符串中字符的信息。愛掏網(wǎng) - it200.com
count_chars(string,mode);
參數(shù) | 描述 | 必需/可選 |
---|---|---|
string | 要檢查的字符串 | 必需 |
mode | 指定返回模式 | 可選 |
注意:根據(jù)’mode’參數(shù)的不同,’count_chars()’函數(shù)返回以下之一:
- 0:返回一個以字節(jié)值為鍵,每個字節(jié)頻率為值的數(shù)組。愛掏網(wǎng) - it200.com
- 1:與0相同,但只列出頻率大于零的字節(jié)值。愛掏網(wǎng) - it200.com
- 2:與0相同,但只列出頻率等于零的字節(jié)值。愛掏網(wǎng) - it200.com
- 3:返回一個包含所有唯一字符的字符串。愛掏網(wǎng) - it200.com
- 4:返回一個包含所有未使用字符的字符串。愛掏網(wǎng) - 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)
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。