View Full Version : html in database string
UKSBD
19th November 2009, 07:22
I allow HTML in one of the field/strings in a database.
Is there a way of pulling only a certain ammount of chacaters without it
causing a problem if it breaks half way through HTML code?
Using classic .ASP MySQL
FireFleur
19th November 2009, 07:45
Not without another mechanism coded to sort out that problem.
You could do it this way though:
create table p {
id BIGINT PRIMARY KEY AUTOINCREMENT,
value TEXT
}
then you pull as many characters as you like, but supply the start and end tag.
select '<p>' || substring( p.value, from 1, for 255 ) || '</p>' from p where id = 1;
|| is a concatenation, and the syntax is postgresql but you should be able to translate into whatever.
If a value contains HTML markup though, then it may still not work. You would need the value to be text, etc. You could filter after perhaps, or build a function to scan for markup and drop it.
Astaroth
20th November 2009, 13:57
Are you allowing all types of HTML or only certain tags like <h1> and <b> etc?
If you are allowing a totally free hand then it is going to be difficult to pull out only the first X characters and ensure that it still displays correctly etc as you may pull out half the first row of a table etc.
Is the intention to give a summary? if it is then generally would strip all HTML from it and have it as plain text or allow users to write their own summary and only allow basic HTML tags
UKSBD
20th November 2009, 15:28
Yes it is all HTML
I was wondering if it is possible to pull it from the database as a html page
but then copy as plain text and create a 2nd field/string in the database, then use some of that for a summary.
I.e
as though right clicking over the text and copying, so it copies as plain
text even thoufg there are links in the text.
Is that possible?
FireFleur
20th November 2009, 15:41
You could use a regexp to take out any tags.
s/<[^>]*>//g
asp should have a regex replace function (you may need to change the syntax a little).
Effectively you filter it in the database or via asp, whichever you find easier, database would probably be a little quicker though.
Astaroth
20th November 2009, 15:58
Would agree with FireFleur though as you are removing all tags if the user has included tabled info/ data then it will appear as a continuous string without the tags