Robson » Code » PHP » Sortfile

 
<?
   // sort file script v1.0
 
   $filename = 'filename.txt';
   $lines = file($filename);
   sort($lines);
 
   // this code can be used to add something to the start of each line
   // for example, it could be used to convert code to comments
   /*
   // loop through all lines
   for ($n = 0; $n < count($lines); $n++)
       // add a hash sign at the start
       $lines[$n] .= '#';
   */
 
   $fp = fopen($filename . '.new', 'wb');
   fwrite($fp, implode('', $lines));
   fclose($fp);
 
   echo '<em>' . $filename . '</em> sorted and saved as <em>' . $filename . '.new</em>';
?>
 
© 2004-17 robson | cc unless stated