這篇文章主要介紹了PHP讀取文字檔並逐行輸出該行使用最多的字元與對應次數的方法,涉及php針對檔案的讀取及字串遍歷、統計、排序等相關操作技巧
本文實例講述了PHP讀取文字檔案並逐行輸出該行使用最多的字元與對應次數的方法。分享給大家參考,具體如下:
test.txt檔案:
1 2 3 | Welcome to our website jb51.net
www.jb51.net
php asp java jsp
|
登入後複製
php程式碼(讀取test.txt檔案):
1 2 3 4 5 6 7 8 | $myfile = fopen ( "test.txt" , "r" );
while (! feof ( $myfile )) {
$line_str = fgets ( $myfile );
$str_arr = count_chars ( $line_str , 1);
arsort( $str_arr );
print_r( chr (key( $str_arr )).' is '.current( $str_arr ).PHP_EOL);
}
fclose( $myfile );
|
登入後複製
運作結果如下:
以上是php 讀取文字檔案並逐行輸出該行使用最多的字元與對應次數的方法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!