Thanks.
--
Posted via http://www.ruby-forum.com/.
Shorter, cleaner, more efficient, whitespace-preserving, and uses the HTML
wbr tag (which is a hint for word breaking and is supported well in FF and
IE, though poorly in Safari and not at all in Opera):
def split_str(str, len = 10)
fragment = /.{#{len}}/
str.split(/(\s+)/).map! { |word|
(/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
}.join
end
Note that this will put a wbr tag at the end of words which have a length
evenly divisible by len; these can be removed with another gsub if desired,
but they are harmless. Note also that Rails does some annoying rewriting of
HTML output, and changes <wbr /> into <wbr> for no good reason. This
doesn't affect how browsers render things, but it does mean the results are
no longer XHTML-compliant.
--Greg
Bart