Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 84,866 PHP Programmers. There are 1,414 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a PHP Expert
Powered by LivePerson.com

Register to Make This Box Go Away!


Alphabetize lines of a text file

This code reads all the lines in a text file then alphabetizes it and overwrites the file with the alphabetized version

Submitted By: Dezmond
Actions:
Rating:
Views: 249

Language: PHP

Last Modified: March 25, 2008
Instructions: Just replace XXX with path to text file

Snippet


  1. $fh = fopen("XXX", 'r');
  2. $i = 0;
  3. flock($fh, LOCK_SH);
  4. while (!feof($fh)) {
  5.         $data = fgets($fh, 500);
  6.         $array[$i] = $data;
  7.         $i++;
  8. }
  9. flock($fh, LOCK_UN);
  10. fclose($fh);
  11.        
  12. sort($array);
  13. $count = count($array);
  14.        
  15. $gamelist = fopen("XXX", 'w');
  16. flock($gamelist, LOCK_EX);
  17. for($i = 0; $i <= $count; $i++) {
  18.         fwrite($fh, $array[$i]);
  19. }
  20. flock($fh, LOCK_UN);
  21. fclose($fh);

Copy & Paste


Comments


Dezmond 2008-04-04 22:10:58

Well Dezmond, I would like to say that this is, quite possible the best thing ever...


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month