text: Q:
  
  How to I sort a string that is in a .txt file using php?
  
  I would like to display the 'words' in alphabetical order, but all of the examples of solutions that I have come across, are not working for me. 
  I have tried using the implode() and explode() functions, using the file() function, and then using the sort() function. 
  Any help is very much appreciated, thank you!
  <!DOCTYPE html>
  <html>
    <head>
      <title>words list</title>
    </head>
    <body>
      <h1>Word List</h1> 
      <?php
        @$fp = fopen("words.txt", 'rb');
        flock($fp, LOCK_SH); // lock file for reading
  
        while (!feof($fp)) {
           $words= fgets($fp);
       explode($words, "\n");
           file($words);
           sort($words);
  
           echo htmlspecialchars($words)."<br />";
        }
        flock($fp, LOCK_UN); // release read lock
  
        fclose($fp); 
      ?>
    </body>
  </html>
  
  A:
  
  <!DOCTYPE html>
  <html>
    <head>
      <title>words list</title>
    </head>
    <body>
      <h1>Word List</h1> 
      <?php
      $lines = file("words.txt");
      print_r($lines);
      natsort($lines); // this will sort lines in your file
      print_r($lines);
      // This was optional here you can overwrite existing file or can create new file
      file_put_contents("newtime.txt", implode("\n", $lines)); 
      ?>
    </body>
  </html>
  
  I checked and It works for me ! ! !
  
  