Simplified with array_map():
<?php
if(isset($_POST['txt'])) {
$text = $_POST["txt"];
$text = str_replace("\r\n", "\n", $text);
$lines = explode("\n", $text);
$goodLines = array_map('ucfirst', array_map('strtolower', $lines));
$goodText = implode(" \n", $goodLines);
echo nl2br($goodText);
}
?>
Here's a phpfidle that proves it works: