http://www.perlmonks.org?node_id=273997

Q&A > regular expressions > How do I remove whitespace at the beginning or end of my string? - Answer contributed by Mago

Remove whitespace at the beginning and end of your string, as well as consecutive whitespace (more than one whitespace character in a row) throughout the string.
$string = join(' ',split(' ',$string));