- trim: documentation ( source)
- preg_replace: documentation ( source)
<?php
$Text = "There is a pleasure in the pathless woods,
There is a rapture on the lonely shore,
There is society, where none intrudes.";
$Text=trim($Text);
$Text=preg_replace("/[\r\n]+/", "\n", $Text);
echo $Text;