Mark Mark - 4 months ago 13
HTML Question

Simple text to HTML conversion

I have a very simple

asp:textbox
with the
multiline
attribute enabled. I then accept just text, with no markup, from the textbox. Is there a common method by which line breaks and returns can be converted to
<p>
and
<br/>
tags?

I'm not looking for anything earth shattering, but at the same time I don't just want to do something like:

html.Insert(0, "<p>");
html.Replace(Enviroment.NewLine + Enviroment.NewLine, "</p><p>");
html.Replace(Enviroment.NewLine, "<br/>");
html.Append("</p>");


The above code doesn't work right, as in generating correct html, if there are more than 2 line breaks in a row. Having html like
<br/></p><p>
is not good; the
<br/>
can be removed.

Answer

Depending on exactly what you are doing with the content, my typical recommendation is to ONLY use the <br /> syntax, and not to try and handle paragraphs.

Comments