Home  >  Article  >  Backend Development  >  PHP reads a text file and outputs the most used characters and corresponding times in the line line by line. Detailed explanation of method examples

PHP reads a text file and outputs the most used characters and corresponding times in the line line by line. Detailed explanation of method examples

怪我咯
怪我咯Original
2017-07-04 14:20:291583browse

This article mainly introduces the method of PHP reading a text file and outputting the most used characters and corresponding times in the line line by line, involving PHP's reading of the file and StringTraversal, statistics, Sorting and other related operation skills

The example in this article describes the method of PHP reading a text file and outputting the most used characters and the corresponding times in the line line by line. Share it with everyone for your reference, the details are as follows:

test.txt file:

Welcome to our website jb51.net
www.jb51.net
php asp java jsp

php code (read test.txt file):

$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);

The running results are as follows:

 e is 5 w is 3 p is 4

The above is the detailed content of PHP reads a text file and outputs the most used characters and corresponding times in the line line by line. Detailed explanation of method examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn