【发布时间】:2011-06-01 13:36:29
【问题描述】:
我在剥离 HTML 并显示为客户格式化文本时遇到问题。
例如:
asdas<br/>asdas
所以标签将被一个边距替换。但我还需要用空格和制表符替换边距并删除所有标签。是否有任何示例或已完成的解决方案可以在删除 HTML 标记后获得某种格式的文本。
当前解决方案(寻找更好的解决方案):
/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
/// <summary>
/// Compiled regular expression for performance.
/// </summary>
static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
/// <summary>
/// Remove HTML from string with compiled Regex.
/// </summary>
public static string StripAllTagsRegex(string source)
{
source = HttpUtility.HtmlEncode(source);
return _htmlRegex.Replace(source, string.Empty);
}
public static string ChangeTagsToTextFormat(string source)
{
if (string.IsNullOrEmpty(source))
return source;
source = HttpUtility.HtmlEncode(source);
return source.Replace("<br/>", Environment.NewLine)
.Replace("</div>", Environment.NewLine)
.Replace("</p>", Environment.NewLine);
}
}
【问题讨论】:
-
你有没有尝试过什么?
-
我可能正在寻找可以做这些事情的 lib。
-
“边距”是指“换行符”吗?
-
我的意思是
将被换行。真正的 html 边距由空格...