如何将HtmlEncode/HtmlDecode转换为C#中的纯文本?
我使用了CKEditor ASP.NET版本,并根据我的写作空间进行了调整。当单击如何将HtmlEncode/HtmlDecode转换为C#中的纯文本?,c#,asp.net,ckeditor,textarea,html-encode,C#,Asp.net,Ckeditor,Textarea,Html Encode,我使用了CKEditor ASP.NET版本,并根据我的写作空间进行了调整。当单击btn\u Post按钮时,应在此编辑器字段中发布书面文本。我想用C#获取此文本,因为用于在数据库中保存。所以我搜索了如何使用()并找到了使用HtmlEncode的方法。这是我发现的代码 asp 反恐精英 string str=CKEditor1.Text; 字符串str1=Server.HtmlEncode(str); 字符串str2=Server.HtmlDecode(str); //str=1234\r\
btn\u Post
按钮时,应在此编辑器字段中发布书面文本。我想用C#获取此文本,因为用于在数据库中保存。所以我搜索了如何使用()并找到了使用HtmlEncode的方法。这是我发现的代码
asp
反恐精英
string str=CKEditor1.Text;
字符串str1=Server.HtmlEncode(str);
字符串str2=Server.HtmlDecode(str);
//str=1234\r\n
//str1=p1234/p\r\n
//str2=1234\r\n
但问题是,我需要保存没有html代码的文本。如您所见,所有变量都显示html代码。如何将此结果更改为纯文本
1234
?您可以使用此方法
public static string RemoveHTMLTags(string content)
{
var cleaned = string.Empty;
try
{
string textOnly = string.Empty;
Regex tagRemove = new Regex(@"<[^>]*(>|$)");
Regex compressSpaces = new Regex(@"[\s\r\n]+");
textOnly = tagRemove.Replace(content, string.Empty);
textOnly = compressSpaces.Replace(textOnly, " ");
cleaned = textOnly;
}
catch
{
//A tag is probably not closed. fallback to regex string clean.
}
return cleaned;
}
publicstaticstringremovehtmltags(字符串内容)
{
var=string.Empty;
尝试
{
string textOnly=string.Empty;
正则表达式tagRemove=新正则表达式(@“]*(>|$)”;
正则表达式压缩空间=新正则表达式(@“[\s\r\n]+”);
textOnly=tagRemove.Replace(内容,string.Empty);
textOnly=compressSpaces.Replace(textOnly,“”);
清洁=仅文本;
}
抓住
{
//标记可能未关闭。请回退到正则表达式字符串清理。
}
返回清洁;
}
或用于删除所有HTML标记。我希望这将帮助您[删除字符串中的HTML标记][1][如何在ASP.NET中从字符串中删除HTML标记?][2][从字符串中删除HTML标记,包括在C#中的HTML标记][3][1]:[2]:[3]:@RomanBezrabotny Ohh您会发现很多参考资料。谢谢;)我需要更多的阅读只是想知道,如果你只是想要文本,为什么你需要CKEditor?一个简单的文本区不是更好吗?或者,如果您只需要HTML文本之外的纯文本,那么我知道如果找不到
Regex
?\s“always”已经包含\r和\n,那么它们不需要单独包含。(“总是”,因为有些实现可能不包括它们,但所有经常使用的都包括,而常规的.NET实现尤其如此)。
string str = CKEditor1.Text;
string str1 = Server.HtmlEncode(str);
string str2 = Server.HtmlDecode(str);
//str = <p>1234</p>\r\n
//str1 = <p>1234</p>\r\n
//str2 = <p>1234</p>\r\n
public static string RemoveHTMLTags(string content)
{
var cleaned = string.Empty;
try
{
string textOnly = string.Empty;
Regex tagRemove = new Regex(@"<[^>]*(>|$)");
Regex compressSpaces = new Regex(@"[\s\r\n]+");
textOnly = tagRemove.Replace(content, string.Empty);
textOnly = compressSpaces.Replace(textOnly, " ");
cleaned = textOnly;
}
catch
{
//A tag is probably not closed. fallback to regex string clean.
}
return cleaned;
}