Introduction
If you have been into developing Web Applications then you might have at many times experienced that when you display multiple lines of data from a database you loose the spacing or formatting between multiple lines of data. Also in some applications like Forums users can post HTML content directly which can lead to some serious problems. What I mean by Posting HTML content is that e.g.. A user can post a HTML Image tag like <img src="http://myserver.com/mypic.jpg" > and when someone views this post the actual image gets displayed instead of the Tag ! Someone can post a link to some malicious coded page and all the users can become easy targets which can cause some serious security implications.
Problem
The problem that I have described above is divided into 2 parts.
-
Formatting Problem: In HTML all the white spaces between two characters get converted into a single white space automatically. Also Carriage Return '/r' and Line Feed '/n' characters do not have any affect on the HTML formatting. Due to this if you have a multiple line post, while displaying HTML converts all formatting to just a single continuous line.
-
HTML Content: This can be both a problem or boon depending on users of your application. While displaying the content from the database, the HTML engine of the client browser actually parses the HTML content of the data. Due to this instead of displaying the tag's as text, they actually get converted to HTML.
Solution
There is a common solution to both the above problems, you have to parse the Text content from the Database into respective HTML tags.
-
Formatting Solution: In HTML denotes a extra white space. So every 2 white spaces should be substituted by a single white space and .
Also every line terminator should be replaced by the break tag <br>, which will result in the next character starting for a new line. -
HTML Content: The solution to this is a bit tricky, in HTML every valid tag is contained within the < and > brackets. So to make all the HTML tags in your post invalid just change the < and > tags to their HTML counter parts < and > respectively. Also one other formatting change to be made is that the double quotation mark " has to be changed into its HTML equivalent ".
Text to HTML parser
On the .NET Platform the String object is immutable i.e. once you create a String object you cannot change its contents. Since our parser needs to do some heavy weight string manipulations, I use the StringBuilder class from the System.Text namespace which provides a mutable string object. Also for streaming access to textual content I use the StringReader and StringWriter classes from the System.IO namespace.
Click Here to view a sample consumer in action!
Example: Normal post
Some sample text with lots of extra white spacing .. ... and some text on a new line. lastly the HTML textbox tag
Example: Parsed Text with HTML posting allowed
Some sample text with lots of extra white spacing .. ...
and some text on a new line.
lastly the HTML textbox tag
Example: Parsed Text with HTML posting disabled (exactly same as posted)
Some sample text with lots of extra white spacing .. ...
and some text on a new line.
lastly the HTML textbox tag <input type="text">
gold marryPosted Jun 18, 2007, 5:19 AM
public int NumSundays(string month_name, string year_name) { //int i; int num_sundays = 0; System.DateTime test_date; int orig_month; test_date = System.Convert.ToDateTime(month_name + " 1, " + year_name); if (test_date.DayOfWeek == System.DayOfWeek.Sunday) { } else if (test_date.DayOfWeek == System.DayOfWeek.Monday) { test_date = test_date.AddDays(6); } else if (test_date.DayOfWeek == System.DayOfWeek.Tuesday) { test_date = test_date.AddDays(5); } else if (test_date.DayOfWeek == System.DayOfWeek.Wednesday) { test_date = test_date.AddDays(4); } else if (test_date.DayOfWeek == System.DayOfWeek.Thursday) { test_date = test_date.AddDays(3); } else if (test_date.DayOfWeek == System.DayOfWeek.Friday) { test_date = test_date.AddDays(2); } else if (test_date.DayOfWeek == System.DayOfWeek.Saturday) { test_date = test_date.AddDays(1); } orig_month = test_date.Month; do { //num_mondays =num_mondays+ 1; num_sundays++; test_date = test_date.AddDays(7); } while (((test_date.Month) == orig_month)); return num_sundays; }