// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
Google / Android
Python / IronPython
SQL Server
Storing HTML in database and displaying it in a web browser
Table of Contents

The example code needs to be displayed on a web page as it is. For example, if I want the less than symbol to be displayed as it is in the following code fragment:

<b>use b tags to bold the statement</b>

Following should actually be bold: 

use b tags to bold the statement

If you look at the source, you will notice that I am using &lt; to represent < in the first case. In the second case, I actually used the less than symbol. An interesting thing to note is that you do not have to replace the greater than symbol with &gt;. Just change the less than symbol, you will be fine. Similarly, for the following line of code appear with less than and greater than symbols in tact, all you need to do is to replace the less than symbol with &lt;


HtmlEncode and HtmlDecode

If you pass a string through HtmlEncode, it will change all these special HTML symbols into their codes (i.e. < will become &lt;). You can store that in the database and when you obtain the string back to display in a browser, it will render that string with < and > as symbols, not as formatting tags.

If you know you are letting the user enter only text (and no formatting), the above method works perfectly. Meaning, run the user entered string through HtmlEncode and store it in the database and when you retrieve it, pass the string without modifications to the browser (i.e. do not run HtmlDecode). This way, the less than and greater than will not cause any problems in the browser.

If your user entered text is formatted (i.e. it has HTML tags that are to be interpreted as formatting tags) and it could potentially contain < (for example, in code), you need to replace just those symbols. If you are using a simple text box, one way is to ask the user to manually enter &lt; for < and use the HTML tags where the formatting is needed.

Bookmark and Share This

More Articles With Similar Tags
This article talks about the SQLite packages available in Android SDK.
About  Contact  Privacy Policy  Site Map