# Html tags problem in truncated text

**URL:** <https://www.ruby-forum.com/t/html-tags-problem-in-truncated-text/144423>\
**Category:** Rails\
**Created:** [July 29, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/html-tags-problem-in-truncated-text/144423 "2008-07-29T15:43:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [July 29, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/html-tags-problem-in-truncated-text/144423/1 "2008-07-29T15:43:00Z")

</div>

I have problem with truncation of text. My text includes html tags also.  
On main page I display only 100 characters of it, and give read more  
link to user. Now if within these 100 characters, if there is any html  
tag that is opening and not getting closed within 100 characters, then  
that html tag is getting displayed on main page.

How can I solve this problem? Is there any solution for it?

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [July 29, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/html-tags-problem-in-truncated-text/144423/2 "2008-07-29T18:04:44Z")

</div>

On Tue, Jul 29, 2008 at 8:43 AM, Sunny B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have problem with truncation of text. My text includes html tags also.  
> On main page I display only 100 characters of it, and give read more  
> link to user. Now if within these 100 characters, if there is any html  
> tag that is opening and not getting closed within 100 characters, then  
> that html tag is getting displayed on main page.
> 
> How can I solve this problem? Is there any solution for it?

Call strip\_tags before truncating

See:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/SanitizeHelper.html#M001620](http://api.rubyonrails.com/classes/ActionView/Helpers/SanitizeHelper.html#M001620)

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [July 29, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/html-tags-problem-in-truncated-text/144423/3 "2008-07-29T19:22:46Z")

</div>

I’m following this topic merely because I’m interested in the outcome.  
I looked up strip\_tags in the API and it seems that it ‘removes’ the  
tags. I don’t believe that’s what Sunny wants. Truncate take a number  
of characters and appends a … removing the remaining characters that  
may have been there. If the end tag of a is truncated away, then  
all the text from that point on will be bolded. How is this resolved?

~Dustin T.
