Ruby's `.strip` method performs the PHP equivalent to `trim()`.
To remove all whitespace:
" leading trailing ".squeeze(' ').strip
=> "leading trailing"
@Tass made me aware that my original answer removes duplicate letters in succession - YUCK! I've since switched to the squish method which is smarter about such occurrences if using the Rails framework.
require 'active_support/all'
" leading trailing ".squish
=> "leading trailing"
" good men ".squish
=> "good men"
Cite:
[To see links please register here]