PHP count_chars() 函數(shù)
實例
返回一個字符串,包含所有在 "Hello World!" 中使用過的不同字符(模式 3):
<?php $str = "Hello World!"; echo count_chars($str,3); ?>
定義和用法
count_chars()
函數(shù)返回字符串所用字符的信息(例如,ASCII 字符在字符串中出現(xiàn)的次數(shù),或者某個字符是否已經(jīng)在字符串中使用過)。
語法
count_chars( _string,mode_ )
實例 1
返回一個字符串,包含所有在 "Hello World!" 中未使用過的字符(模式 4):
<?php $str = "Hello World!"; echo count_chars($str,4); ?>
實例 2
在本實例中,我們將使用 count_chars() 來檢查字符串,返回模式設置為 1。模式 1 將返回一個數(shù)組,ASCII 值為鍵名,出現(xiàn)的次數(shù)為鍵值:
<?php $str = "Hello World!"; print_r(count_chars($str,1)); ?>
實例 3
統(tǒng)計 ASCII 字符在字符串中出現(xiàn)的次數(shù)另一個實例:
<?php $str = "PHP is pretty fun!!"; $strArray = count_chars($str,1); foreach ($strArray as $key=>$value) { echo "The character <b>'".chr($key)."'</b> was found $value time(s)<br>"; } ?>
總結
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com