Easiest Way To Sort Strings From a File By Alphabet with PHP

By | May 28, 2008

Today I won’t write many letters, I’ll show a simple code that allows to sort file contents by alphabet.

<?

// Getting file into array
$stroki=file(“unsorted.txt”);

//Sorting it without keys – we don’t need them
sort($stroki);

// Writing sorted strings to a new file
$fs=fopen(“./sorted.txt”, “a”);
foreach ($stroki as $string)
{
fwrite($fs, trim($string).”rn”);
}
fclose ($fs);
echo “Completed”;
?>