//編程語言為網絡編程的php腳本語言
header("content-type:text/html; charset=utf-8");
$filePath="txt.txt";
$chars=2;
$wordArray =array();
$file=fopen($filePath,"r");
while(!FEOF($file)){
//讀出壹行
$singleLine=trim(fgets($file));
//數字、英文、標點、空格過濾
$singleLine=preg_replace("/[0-9]{1}/", "", $singleLine);
$singleLine=preg_replace("/[a-zA-Z]{1}/", "", $singleLine);
$singleLine=preg_replace("/[ '.,:;*?~`!@#$%^&+=\-)(<>{}]|\]|\[|\/|\\\|\"|\|/", "", $singleLine);
$singleLine=str_replace(" ", "", $singleLine);
//只處理字數多於2的行
if (strlen($singleLine)>2){
for($i=0;$i<strlen($singleLine)-$chars*3;$i=$i+3){//壹個漢字在utf-8下算三個字符
$word=substr($singleLine,$i,$chars*3);
$wordArray[]=$word;
//echo $word;
}
}
}
//關閉文件
fclose($file);
//對頻數進行統計
$wordArrayOut=array_count_values($wordArray);
//根據統計次數降序排列
arsort($wordArrayOut);
//輸出結果
$i=1;
foreach($wordArrayOut as $key=>$value){
$rankNo=$i<10?"0".$i:$i;
echo "$key $value<br />";
$i++;
}