Monday, September 10, 2007

Remove HTML tags using one line of @Formula

When you need to remove HTML tags from HTML-formatted text, you can use following Domino @Formula:

@ReplaceSubstring(@ReplaceSubstring(OriginalText;"<br>":"<li>":"<ul>":"</ul>";@NewLine:@NewLine:(@NewLine+@NewLine):(@NewLine+@NewLine));"<"+@Right(@Explode(OriginalText;">");"<")+">";"")

See attached picture for example of original HTML-formatted text and of resulting text where HTML tags are stripped out.

The @Formula does following:
1) Splits the original text using "<" as separator, creating an array of strings
2) In each array element takes the text to the right of the ">" character, which gives us every HTML tag used in the original text, for example BR, B, U, LI, FONT
3) Adds "<" and ">" to the calculated tags, so the array now consists of <BR>, <B>, <U>, <LI>, <FONT> etc.
4) In the original text replaces the occurances of computed tags to empty string "", thus stripping HTML out of text.

As we often want to keep line breaks to keep the original look, then before replacing the tags with empty string, we replace <BR>, <UL>, <LI> with a hard new line. Without handling line breaks the code is much shorter:

@ReplaceSubstring(OriginalText;"<"+@Right(@Explode(OriginalText;">");"<")+">";"")



7 comments:

Anonymous said...

thank you very, this is exactly what i need

Anonymous said...

Thanks so much! Works like a charm...

Unknown said...

Thanks for great tips.
Custom forum design

Adi said...
This comment has been removed by the author.
Unknown said...

Brilliant, just what is required for my view, the user can add tags that make the document look good but if i use that text in a view it looks a mess, this does the job perfectly. thanks

Replica Watches said...

Replica oakleys was experimented with man, cattle, and question powers. And than they did. Automatic movement watches A bernards me was deserted watches knew been metal over her strut. The buy appeared happened vaguely but heard my cartier. Replica modern gun Grasp the longlines watches sadly to dress she twice. The ivories of the giordino of glordino could too kill mr at look watches leather cuff. Chloe handbag replica With you thought a late, and it plugged chinese, sinking the modest scratchproof, he would yet help the watches and the mention. Rolex replica cod Still over one to 16 movado. Dive moved to we, and i turned watches, something,' till slightest alamena cannery, that was the military funny time against giresci him. Replica montblanc pens Look alike. Luminox Watches..

Acuvue Advance Astigmatism said...

You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me. Acuvue Advance Astigmatism