Home > Backend Development > PHP Tutorial > 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

怪我咯
Release: 2023-03-12 16:14:01
Original
1594 people have browsed it

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
Copy after login

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);
Copy after login

The running results are as follows:

 e is 5 w is 3 p is 4
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template