[man]trim/man will work for most cases... except for the non-breaking space character I talked about above. Many, many applications overlook this space character (I've found many bugs/exploits/loopholes/etc. using this character :p). For example, this code:
<?php
header('Content-Type: text/plain');
$str = "**************"; // these are non-breaking spaces, Unicode character U+00A0
echo '|' . trim($str) . "|\n";
echo '|' . preg_replace('/\s/', '', $str) . "|\n";
outputs:
| |
||
(Note that in my code above, the vBulletin software replaces the Unicode characters with an asterisk.)
EDIT: Note that the above statement about trim() holds true for PHP 5.3.2 and prior. It's possible that PHP 6 (which is supposedly more Unicode-centric/-friendly/-aware) might be adjusted to look for this Unicode character as well.