简单的文本到HTML的转换 [英] Simple text to HTML conversion

查看:76
本文介绍了简单的文本到HTML的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的asp:textbox,其中启用了multiline属性.然后,我从文本框中仅接受没有标记的文本.是否有一种常见的方法可以将换行符和返回符转换为<p><br/>标记?

我不希望有任何破坏地球的事情,但与此同时,我不只是想做类似的事情:

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

上面的代码无法正常工作,如在生成正确的html中,如果一行中有两个以上的换行符.像<br/></p><p>这样的html不好; <br/>可以删除.

解决方案

具体取决于您对内容的处理方式,我的典型建议是仅使用<br />语法,而不要尝试处理段落. /p>

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.

解决方案

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.

这篇关于简单的文本到HTML的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆