# Long words killing HTML display

**URL:** <https://www.ruby-forum.com/t/long-words-killing-html-display/75219>\
**Category:** Rails\
**Created:** [November 8, 2006, 9:25am UTC](https://www.ruby-forum.com/t/long-words-killing-html-display/75219 "2006-11-08T09:25:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [November 8, 2006, 9:25am UTC](https://www.ruby-forum.com/t/long-words-killing-html-display/75219/1 "2006-11-08T09:25:55Z")

</div>

Hi, I’m trying to use word\_wrap to prevent long  
‘aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa…’ from kill my display  
tables in HTML. Problem is that word\_wrap doesn’t dissect these long  
words and expects to only break them if it sees a ‘\n’. Does anybody  
know if Rails has a helper to do this, not that it is hard to implement  
on my own but jsut wondering if there’s something really clean that  
takes care of this and anything more.

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [November 8, 2006, 9:40am UTC](https://www.ruby-forum.com/t/long-words-killing-html-display/75219/2 "2006-11-08T09:40:09Z")

</div>

Had the same problem so I wrote this method: put it into your helpers  
and application.rb. There might be a cleaner way using Regexp or  
scan… this uses String.split()  
str = the string to split  
len = longest length of each piece  
char = character to split them with

# - split a long string into smaller pieces, at most “len” characters

# in length. “len” default is 10, if not supplied.

# - returns the split string

def split\_str(str=nil, len=10, char=" ")  
len = 10 if len \< 1  
work\_str = str.to\_s.split(//) if str  
return\_str = “”  
i = 0  
if work\_str  
work\_str.each do |s|  
if (s == char || i == len)  
return\_str += char  
return\_str += s if s != char  
i = 0  
else  
return\_str += s  
i += 1  
end  
end  
end  
return\_str  
end

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [November 8, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/long-words-killing-html-display/75219/3 "2006-11-08T16:09:31Z")

</div>

Hey, I said there’s probobly a better way to do it. Thanks Greg. I’ll  
put your slimmed down version in.

Bart

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [November 8, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/long-words-killing-html-display/75219/4 "2006-11-08T14:55:12Z")

</div>

On Wed, Nov 08, 2006 at 12:37:32AM -0800, Bart wrote:  
} Had the same problem so I wrote this method: put it into your helpers  
} and application.rb. There might be a cleaner way using Regexp or  
} scan… this uses String.split()  
} str = the string to split  
} len = longest length of each piece  
[19 lines of code elided]

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’)  
}.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 into for no good reason. This  
doesn’t affect how browsers render things, but it does mean the results  
are  
no longer XHTML-compliant.

–Greg
