大家好,我正在尝试获取已在所见即所得编辑器中输入的描述并获取它的子字符串。
i.e
This is some <span style="font-weight:bold;">text</span>
如果我只是子串并添加...,我想在不破坏html的情况下限制一些描述
它破坏了 html 标签..
我试过了:
string HtmlSubstring(string html, int maxlength)
{
string htmltag = "</?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/?>";
string emptytags = "<(\\w+)((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)/?></\\1>";
var expression = new Regex(string.Format("({0})|(.?)", htmltag));
MatchCollection matches = expression.Matches(html);
int i = 0;
StringBuilder content = new StringBuilder();
foreach (Match match in matches)
{
if (match.Value.Length == 1 && i < maxlength)
{
content.Append(match.Value);
i++;
}
else if (match.Value.Length > 1)
{
content.Append(match.Value);
}
}
return Regex.Replace(content.ToString(), emptytags, string.Empty);
}
但它并没有完全让我到达那里!
Use the HTML 敏捷包 http://html-agility-pack.net加载 HTML,然后获取 InnerText。
var document = new HtmlDocument();
document.LoadHtml("...");
document.DocumentNode.InnerText;
另请参阅C#:HtmlAgilityPack 提取内部文本 https://stackoverflow.com/questions/2785092/c-htmlagilitypack-extract-inner-text
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)