Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncating An HTML String

Status
Not open for further replies.

TheVillageIdiot27

Programmer
Nov 10, 2005
58
GB
I have a grid view on a web form (c#) in which there is a template field which is linked to a comments field in an SQL database which contains text formatted as an HTML fragment.

This field can get quite long so I am trying to return a substring of the this formatted HTML (for example the first 50 characters).

What I seem unable to do is to ensure the markup of the HTML remains intact as it is truncated and I am not left with half a tag, an unclosed bold tag or similar.

If anyone has any ideas I would gratefully receive them.
 
And what about the rendering? Are you displaying the HTML or rendering it?

In any case you would need to make sure that in the encoded string that you have an equal number of <
and > up to a certain length (e.g 50 characters).

The problem will be that you may never actually get an equal amount of these tags within a range that you are happy with. For example, what happens if there are 10 nested, opening <div> tags in the first 50 characters of the string? You would then have at least another 50 characters before you would reach the end of all of those tags.


Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
At the moment it looks like this
Code:
<asp:Literal ID="LiteralCommentary" runat="server" Text='<%# Eval("incidents_commentary") %>' />

using a regular expression I can work out what the string would be with no mark up

Code:
    private string RemoveHTMLTags(string strHTMLFragement)
    {
            
        string strExpn = "<.*?>";
        return Regex.Replace(strHTMLFragement, strExpn, string.Empty);        
    }

which given the issues you have already mentioned may be enough to simply to return a substring of that with no tags in it at all.

however I think it may be better if I could return mark up where possible. I am unfamiliar with the HtmlTextWriter or Html32TextWriter classes but wonder if there is a solution within those.

 
This sounds like a design issue. you are storing formatted text in the database, but you also want to alter the formatting (show partial text) in certain scenarios.

do you have the option to alter how you store the text? if so you have some options.
1. store pre-formatted text and apply formatting when presenting to the user
2. store the full formatted version (what you have currently) and a "teaser" formatted version (truncated text). then you can choose which one to present to the user.
3. continue with your current solution of using regex to parse out the html formatting. This seems like more trouble than it's worth though.

I believe there are very few reasons to store formatted text in the database as formatting is strictly a UI concern. It limits how that text can be utilized with the system. It also locks you into a specific format unless you run an ETL process against the data anytime the formatting changes.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
By rendering, I mean from a user's point of view what will they see? If your string is <b>Hello<b>, will they see that string or will they just see Hello in a bold font?

Yes, it's easy to truncate the string down to 50 characters, but if you require that 50 characters to contain valid HTML then that will be very hard to enforce. If you decide to go with this approach, you may want to consider doing this in your stored procedure rather than returning all of the data only to then strip it down.

Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
Sorry Mark - they would see hello in a bold font.

I am thinking that I will probably present the teaser unformatted then have the formatted version displayed on mouse over.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top