Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, August 6, 2016

Simple text to HTML conversion

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

and
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, "

"); html.Replace(Enviroment.NewLine + Enviroment.NewLine, "

"); html.Replace(Enviroment.NewLine, "
"); html.Append("

");

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

is not good; the
can be removed.

Answer by Klaus Byskov Pedersen for Simple text to HTML conversion


How about throwing it in a

 tag. Isn't that what it's there for anyway?

Answer by Mitchel Sellers for Simple text to HTML conversion


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

Answer by Paul Sasik for Simple text to HTML conversion


Your other option is to take the text box contents and instead of trying for line a paragraph breaks just put the text between PRE tags. Like this:

  Your text from the text box...    and a line after a break...  

Answer by KGC for Simple text to HTML conversion


I know this is an old post, but I've recently been in a similar problem using C# with MVC4, so thought I'd share my solution.

We had a description saved in a database. The text was a direct copy/paste from a website, and we wanted to convert it into semantic HTML, using

tags. Here is a simplified version of our solution:

string description = getSomeTextFromDatabase();  foreach(var line in description.Split('\n')  {      Console.Write("

" + line + "

"); }

In our case, to write out a variable, we needed to prefix @ before any variable or identifiers, because of the Razor syntax in the ASP.NET MVC framework. However, I've shown this with a Console.Write, but you should be able to figure out how to implement this in your specific project based on this :)

Answer by eselk for Simple text to HTML conversion


I know this is old, but I couldn't find anything better after some searching, so here is what I'm using:

public static string TextToHtml(string text)  {      text = HttpUtility.HtmlEncode(text);      text = text.Replace("\r\n", "\r");      text = text.Replace("\n", "\r");      text = text.Replace("\r", "
\r\n"); text = text.Replace(" ", "  "); return text; }

If you can't use HttpUtility for some reason, then you'll have to do the HTML encoding some other way, and there are lots of minor details to worry about (not just <>&).

HtmlEncode only handles the special characters for you, so after that I convert any combo of carriage-return and/or line-feed to a BR tag, and any double-spaces to a single-space plus a NBSP.

Optionally you could use a PRE tag for the last part, like so:

public static string TextToHtml(string text)  {      text = "
" + HttpUtility.HtmlEncode(text) + "
"; return text; }


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.