<?php
$test = <<<EOD
This is some text
This is more text
That was an empty line
There were a couple more
This is a line with whitespace at the beginning
Here are some empties at the end
EOD;
echo "'".preg_replace('/^[\r\n]+|[\r\n]+$/m', '', $test)."'";