- preg_replace: documentation ( source)
<?php
$string = '
The quick brown fox jumped over the lazy dog.
';
$string = preg_replace('/^\s*/', '', $string);
$string = preg_replace('/\s*$/', '', $string);
echo $string;
?>