"Joe Conlin" <jo*@bellyusa.com> wrote in message
news:dh***********************@twister2.starband.n et...
I am truncating data from an SQL database and have a script that populates
a cell. The problem that I have is I have HTML tags in my database so if the
data gets truncated in the middle of a tag, then my page goes...well, you
know. What I am looking for is a way that I can contain ANY data (i.e.
characters, tags, etc.) directly in that cell. So...If I have a <table>
tag it will not throw off the rest of the table tags throughout my page.
I can think of two different things you might have in mind:
1. You want the data to be treated as actual HTML inside the table cell. For
example, if your data (after truncation) were:
This is the <em>last</em> time that I will ever trav
then you would want this string to be displayed with the word "last"
italicized (assuming that is how the browser handles <em>), and without
explicitly displaying the <em> and </em> tags.
2. You want to display HTML tags from the data literally. Using the sample
string above, you would want to display, literally,
This is the <em>last</em> time that I will ever trav
with explicit tags, and without any change in the appearance of the word
"last".
In the latter case, you're escaping your data anyway, replacing < with <
and > with >. Once you've done that, there's no problem: it won't matter
whether the truncation occurs between opening and closing tags, or within a
tag.
In the former case, I think you're out of luck. Depending on whether there
are any restrictions on the tags you permit to be stored in the data in the
first place, your problems may go beyond the ones you mentioned. What if the
data includes tags that can't appear inside a TD, like <head> or <title> or
<body>? What if there's an <img> tag whose source is a relative URL that has
no meaning from the perspective of your web site? Unless you develop some
extraordinarily sophisticated code to reduce your data to something that can
appear properly inside a TD, as well as accounting for and filling in
missing end tags and even missing tag *parts*, you're going to have trouble.
The only practical solution I can think of is to filter out *all* tags and
just display plain text. (In that case, you would probably filter first,
then truncate.) This is what search engines do when they display excerpts
from pages that match your query.