The one line was just missing a closing paren. I haven't tested any of this. If you wanted to, you culd manually strip and replace the CRLF's.
$file = '/path/to/file.txt';
$new = array(); # We'll store the good content here
if(($array = file($file)) !== false) {
# we have an array
foreach($array as $key => $line) {
# loop through and remove blank lines
$line = str_replace("\n", "", $line);
$line = str_replace("\r", "", $line);
if(!empty($line)) {
$new[] = $line; # not empty, add to new array
}
}
# natural order sort
natsort($new); # sort new array
# open and write
if(($handle = fopen($file, 'w+')) !== false) {
fwrite($handle, implode("\n", $new)); # Implode with newlines
fclose($handle);
}
else {
echo "Couldn't open ".$file." for writing";
}
}
else {
echo "Couldn't read ".$file;
}