PHP reads a text file and prints the line-by-line method with the most characters and number of times _php tips

Source: Internet
Author: User
Tags php code php regular expression

This article describes the way php reads a text file and prints out the most characters and the number of times the row is used. Share to everyone for your reference, specific 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 (Key ($str _arr)). "Is". Current ($str _arr). PHP_EOL);
}
Fclose ($myfile);

The results of the operation are as follows:

 E is 5 W are 3 p is 4

For more information on PHP related content readers can view the site topics: "Summary of PHP file operations", "PHP string (String) Usage summary", "PHP common traversal algorithm and skills summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented Programming", "PHP+MYSQL Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.