count_chars
返回有關字符串中使用的字符的信息-統計string 中每個字節值(0..255)出現的次數
count_chars()
函數返回字符串中所用字符的信息(例如,ASCII 字符在字符串中出現的次數,或者某個字符是否已經在字符串中使用過)。
返回一個字符串,包含所有在"Hello World!" 中使用過的不同字符(模式3):
<?php $str = "Hello World!" ; echo count_chars ( $str , 3 ) ; ?>
親自試一試
返回一個字符串,包含所有在"Hello World!" 中未使用過的字符(模式4):
<?php $str = "Hello World!" ; echo count_chars ( $str , 4 ) ; ?>
親自試一試
在此實例中,我們將使用count_chars() 來檢查字符串,返回模式設置為1。模式1 將返回一個數組,ASCII 值為鍵名,出現的次數為鍵值:
<?php $str = "Hello World!" ; print_r ( count_chars ( $str , 1 ) ) ; ?>
親自試一試
統計某個ASCII 字符在字符串中出現次數的另一個實例:
<?php $str = "PHP is pretty fun!!" ; $strArray = count_chars ( $str , 1 ) ; foreach ( $strArray as $key => $value ) { echo "字符<b>'" . chr ( $key ) . "'</b> 被找到$value次。<br>" ; } ?>
親自試一試
count_chars ( string , mode )
參數 | 描述 |
---|---|
string | 必需。規定要檢查的字符串。 |
mode |
可選。規定返回模式。默認是0。以下是不同的返回模式:
|