有没有人有这个变种?
这是我可以采取一些HTML并显示它而不打破作为文章的摘要引导?
让我免于重新发明轮子!
对不起,新来的,你的权利,应该更好地表达这个问题,还有更多的信息
我希望获取一个html字符串并将其截断为一定数量的单词(甚至是char长度),这样我就可以将它的开头显示为摘要(然后导致主要文章).我希望保留html,以便我可以在预览中显示链接等.
我必须解决的主要问题是,如果我们在一个或多个标签的中间截断,我们最终可能会得到未封闭的html标签!
我有解决方案的想法是
首先将html截断为N个单词(单词更好但chars ok)(确保不要停留在标记的中间并截断require属性)
通过这个截断的字符串中打开的html标签工作(也许我会把它们粘在堆栈上?)
然后处理结束标记并确保它们与我在弹出它们时的堆栈匹配?
如果在此之后有任何打开的标签留在堆栈上,那么将它们写入截断的字符串的末尾,html应该是好的去!
感谢所有评论:)
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PINET40TestProject
{
[TestClass]
public class UtilityUnitTest
{
public static string TruncateHTMLSafeishChar(string text, int charCount)
{
bool inTag = false;
int cntr = 0;
int cntrContent = 0;
// loop through html, counting only viewable content
foreach (Char c in text)
{
if (cntrContent == charCount) break;
cntr++;
if (c == '<')
{
inTag = true;
continue;
}
if (c == '>')
{
inTag = false;
continue;
}
if (!inTag) cntrContent++;
}
string substr = text.Substring(0, cntr);
//search for nonclosed tags
MatchCollection openedTags = new Regex("<[^/](.|\n)*?>").Matches(substr);
MatchCollection closedTags = new Regex("<[/](.|\n)*?>").Matches(substr);
// create stack
Stack<string> opentagsStack = new Stack<string>();
Stack<string> closedtagsStack = new Stack<string>();
// to be honest, this seemed like a good idea then I got lost along the way
// so logic is probably hanging by a thread!!
foreach (Match tag in openedTags)
{
string openedtag = tag.Value.Substring(1, tag.Value.Length - 2);
// strip any attributes, sure we can use regex for this!
if (openedtag.IndexOf(" ") >= 0)
{
openedtag = openedtag.Substring(0, openedtag.IndexOf(" "));
}
// ignore brs as self-closed
if (openedtag.Trim() != "br")
{
opentagsStack.Push(openedtag);
}
}
foreach (Match tag in closedTags)
{
string closedtag = tag.Value.Substring(2, tag.Value.Length - 3);
closedtagsStack.Push(closedtag);
}
if (closedtagsStack.Count < opentagsStack.Count)
{
while (opentagsStack.Count > 0)
{
string tagstr = opentagsStack.Pop();
if (closedtagsStack.Count == 0 || tagstr != closedtagsStack.Peek())
{
substr += "</" + tagstr + ">";
}
else
{
closedtagsStack.Pop();
}
}
}
return substr;
}
public static string TruncateHTMLSafeishWord(string text, int wordCount)
{
bool inTag = false;
int cntr = 0;
int cntrWords = 0;
Char lastc = ' ';
// loop through html, counting only viewable content
foreach (Char c in text)
{
if (cntrWords == wordCount) break;
cntr++;
if (c == '<')
{
inTag = true;
continue;
}
if (c == '>')
{
inTag = false;
continue;
}
if (!inTag)
{
// do not count double spaces, and a space not in a tag counts as a word
if (c == 32 && lastc != 32)
cntrWords++;
}
}
string substr = text.Substring(0, cntr) + " ...";
//search for nonclosed tags
MatchCollection openedTags = new Regex("<[^/](.|\n)*?>").Matches(substr);
MatchCollection closedTags = new Regex("<[/](.|\n)*?>").Matches(substr);
// create stack
Stack<string> opentagsStack = new Stack<string>();
Stack<string> closedtagsStack = new Stack<string>();
foreach (Match tag in openedTags)
{
string openedtag = tag.Value.Substring(1, tag.Value.Length - 2);
// strip any attributes, sure we can use regex for this!
if (openedtag.IndexOf(" ") >= 0)
{
openedtag = openedtag.Substring(0, openedtag.IndexOf(" "));
}
// ignore brs as self-closed
if (openedtag.Trim() != "br")
{
opentagsStack.Push(openedtag);
}
}
foreach (Match tag in closedTags)
{
string closedtag = tag.Value.Substring(2, tag.Value.Length - 3);
closedtagsStack.Push(closedtag);
}
if (closedtagsStack.Count < opentagsStack.Count)
{
while (opentagsStack.Count > 0)
{
string tagstr = opentagsStack.Pop();
if (closedtagsStack.Count == 0 || tagstr != closedtagsStack.Peek())
{
substr += "</" + tagstr + ">";
}
else
{
closedtagsStack.Pop();
}
}
}
return substr;
}
public static string TruncateHTMLSafeishCharXML(string text, int charCount)
{
// your data, probably comes from somewhere, or as params to a methodint
XmlDocument xml = new XmlDocument();
xml.LoadXml(text);
// create a navigator, this is our primary tool
XPathNavigator navigator = xml.CreateNavigator();
XPathNavigator breakPoint = null;
// find the text node we need:
while (navigator.MoveToFollowing(XPathNodeType.Text))
{
string lastText = navigator.Value.Substring(0, Math.Min(charCount, navigator.Value.Length));
charCount -= navigator.Value.Length;
if (charCount <= 0)
{
// truncate the last text. Here goes your "search word boundary" code:
navigator.SetValue(lastText);
breakPoint = navigator.Clone();
break;
}
}
// first remove text nodes, because Microsoft unfortunately merges them without asking
while (navigator.MoveToFollowing(XPathNodeType.Text))
{
if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After)
{
navigator.DeleteSelf();
}
}
// moves to parent, then move the rest
navigator.MoveTo(breakPoint);
while (navigator.MoveToFollowing(XPathNodeType.Element))
{
if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After)
{
navigator.DeleteSelf();
}
}
// moves to parent
// then remove *all* empty nodes to clean up (not necessary):
// TODO, add empty elements like <br />, <img /> as exclusion
navigator.MoveToRoot();
while (navigator.MoveToFollowing(XPathNodeType.Element))
{
while (!navigator.HasChildren && (navigator.Value ?? "").Trim() == "")
{
navigator.DeleteSelf();
}
}
// moves to parent
navigator.MoveToRoot();
return navigator.InnerXml;
}
[TestMethod]
public void TestTruncateHTMLSafeish()
{
// Case where we just make it to start of HREF (so effectively an empty link)
// 'simple' nested none attributed tags
Assert.AreEqual(@"<h1>1234</h1><b><i>56789</i>012</b>",
TruncateHTMLSafeishChar(
@"<h1>1234</h1><b><i>56789</i>012345</b>",
12));
// In middle of a!
Assert.AreEqual(@"<h1>1234</h1><a href=""testurl""><b>567</b></a>",
TruncateHTMLSafeishChar(
@"<h1>1234</h1><a href=""testurl""><b>5678</b></a><i><strong>some italic nested in string</strong></i>",
7));
// more
Assert.AreEqual(@"<div><b><i><strong>1</strong></i></b></div>",
TruncateHTMLSafeishChar(
@"<div><b><i><strong>12</strong></i></b></div>",
1));
// br
Assert.AreEqual(@"<h1>1 3 5</h1><br />6",
TruncateHTMLSafeishChar(
@"<h1>1 3 5</h1><br />678<br />",
6));
}
[TestMethod]
public void TestTruncateHTMLSafeishWord()
{
// zero case
Assert.AreEqual(@" ...",
TruncateHTMLSafeishWord(
@"",
5));
// 'simple' nested none attributed tags
Assert.AreEqual(@"<h1>one two <br /></h1><b><i>three ...</i></b>",
TruncateHTMLSafeishWord(
@"<h1>one two <br /></h1><b><i>three </i>four</b>",
3), "we have added ' ...' to end of summary");
// In middle of a!
Assert.AreEqual(@"<h1>one two three </h1><a href=""testurl""><b class=""mrclass"">four ...</b></a>",
TruncateHTMLSafeishWord(
@"<h1>one two three </h1><a href=""testurl""><b class=""mrclass"">four five </b></a><i><strong>some italic nested in string</strong></i>",
4));
// start of h1
Assert.AreEqual(@"<h1>one two three ...</h1>",
TruncateHTMLSafeishWord(
@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i>",
3));
// more than words available
Assert.AreEqual(@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i> ...",
TruncateHTMLSafeishWord(
@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i>",
99));
}
[TestMethod]
public void TestTruncateHTMLSafeishWordXML()
{
// zero case
Assert.AreEqual(@" ...",
TruncateHTMLSafeishWord(
@"",
5));
// 'simple' nested none attributed tags
string output = TruncateHTMLSafeishCharXML(
@"<body><h1>one two </h1><b><i>three </i>four</b></body>",
13);
Assert.AreEqual(@"<body>\r\n <h1>one two </h1>\r\n <b>\r\n <i>three</i>\r\n </b>\r\n</body>", output,
"XML version, no ... yet and addeds '\r\n + spaces?' to format document");
// In middle of a!
Assert.AreEqual(@"<h1>one two three </h1><a href=""testurl""><b class=""mrclass"">four ...</b></a>",
TruncateHTMLSafeishCharXML(
@"<body><h1>one two three </h1><a href=""testurl""><b class=""mrclass"">four five </b></a><i><strong>some italic nested in string</strong></i></body>",
4));
// start of h1
Assert.AreEqual(@"<h1>one two three ...</h1>",
TruncateHTMLSafeishCharXML(
@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i>",
3));
// more than words available
Assert.AreEqual(@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i> ...",
TruncateHTMLSafeishCharXML(
@"<h1>one two three </h1><a href=""testurl""><b>four five </b></a><i><strong>some italic nested in string</strong></i>",
99));
}
}
}
Run Code Online (Sandbox Code Playgroud)
Abe*_*bel 11
编辑:请参阅下面的完整解决方案,第一次尝试剥离HTML,第二次尝试剥离HTML
让我们总结一下你想要的东西:
<body>如果你的HTML是XHTML,这就变得微不足道了(而且,虽然我还没有看到PHP解决方案,但我很怀疑他们使用类似的方法,但我相信这是可以理解的而且非常简单):
XmlDocument xml = new XmlDocument();
// replace the following line with the content of your full XHTML
xml.LoadXml(@"<body><p>some <i>text</i>here</p><div>that needs stripping</div></body>");
// Get all textnodes under <body> (twice "//" is on purpose)
XmlNodeList nodes = xml.SelectNodes("//body//text()");
// loop through the text nodes, replace this with whatever you like to do with the text
foreach (var node in nodes)
{
Debug.WriteLine(((XmlCharacterData)node).Value);
}
Run Code Online (Sandbox Code Playgroud)
注意:将保留空格等.这通常是件好事.
如果你没有XHTML,你可以使用HTML Agility Pack,让你对普通的旧HTML(它在内部将它转换为某个DOM)做同样的事情.我没试过,但它应该运行得相当顺利.
大编辑:
在一点点评论中,我承诺采用XHTML/XmlDocument方法,并将其用于类型安全方法,以便根据文本长度拆分HTML,但保留HTML代码.我采用了以下HTML,代码在中间正确分解needs,删除其余部分,删除空节点并自动关闭任何打开的元素.
示例HTML:
<body>
<p><tt>some<u><i>text</i>here</u></tt></p>
<div>that <b><i>needs <span>str</span>ip</i></b><s>ping</s></div>
</body>
Run Code Online (Sandbox Code Playgroud)
代码,测试和使用任何类型的输入(好的,授予,我只是做了一些测试和代码可能包含错误,让我知道,如果你找到它们!).
// your data, probably comes from somewhere, or as params to a method
int lengthAvailable = 20;
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"place-html-code-here-left-out-for-brevity");
// create a navigator, this is our primary tool
XPathNavigator navigator = xml.CreateNavigator();
XPathNavigator breakPoint = null;
string lastText = "";
// find the text node we need:
while (navigator.MoveToFollowing(XPathNodeType.Text))
{
lastText = navigator.Value.Substring(0, Math.Min(lengthAvailable, navigator.Value.Length));
lengthAvailable -= navigator.Value.Length;
if (lengthAvailable <= 0)
{
// truncate the last text. Here goes your "search word boundary" code:
navigator.SetValue(lastText);
breakPoint = navigator.Clone();
break;
}
}
// first remove text nodes, because Microsoft unfortunately merges them without asking
while (navigator.MoveToFollowing(XPathNodeType.Text))
if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After)
navigator.DeleteSelf(); // moves to parent
// then move the rest
navigator.MoveTo(breakPoint);
while (navigator.MoveToFollowing(XPathNodeType.Element))
if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After)
navigator.DeleteSelf(); // moves to parent
// then remove *all* empty nodes to clean up (not necessary):
// TODO, add empty elements like <br />, <img /> as exclusion
navigator.MoveToRoot();
while (navigator.MoveToFollowing(XPathNodeType.Element))
while (!navigator.HasChildren && (navigator.Value ?? "").Trim() == "")
navigator.DeleteSelf(); // moves to parent
navigator.MoveToRoot();
Debug.WriteLine(navigator.InnerXml);
Run Code Online (Sandbox Code Playgroud)
代码按以下顺序执行以下操作:
>等一个字符.XPathNavigator这一点,因为我们需要记住这个"突破点".DeleteSelf很方便,但将导航器位置移动到其父级,这就是为什么我们需要根据上一步中记住的"断点"位置检查当前位置.InnerXml.这一切都很简单,虽然看起来有点令人生畏.
PS:如果您使用XSLT,这将是更容易阅读和理解的方式,这是此类工作的理想工具.
更新:添加扩展代码示例,基于已编辑的问题
更新:添加了一些解释