现在的问题是关于内容页中的 html 代码。如果实现分页,容易被截断出现问题,请问这个问题如何解决。谢谢各位!
解决方案 »
- ASP.NET中关于类型转换的问题
- 求一个.net多线程读写sqlserver的例子
- 有没有只讲做论坛的书?只讲一个项目的
- how to insert image into oracle database?
- 大家来帮帮忙,"你无权查看该网页!"在线等!
- 没分了!多谢了...
- 关于用web.config设置文件夹的访问权限
- 怎样用从一个aspx文件 Redirect到另一个aspx文件!!
- 如何象EXECL一样,可以在ASP.NET中添加引用?
- 请问我在treeview中加入了HTML中的CHECKBOX,在后台如何捕获。
- 请问一下如何获取当前页面某个webcontrol的引用
- C#/AJAX/JS/JQUERY/CSS 应有尽有,最近在整理些资料,献给大家了
或过滤HTML分页
手动分页就是在你文章中添加一些标识例如“111111 <#page#>111112”判断的时候发现<#page#>就分页。
//content 内容 length 长度 StripHTML 是否除掉HTML来分割
public static string GetContentSummaryEN(string content, int length, bool StripHTML)
{ string NowCheckLength = "";
if (length < content.Length)
{
NowCheckLength = "1";
}
if (string.IsNullOrEmpty(content) || length == 0)
return "";
if (StripHTML)
{
Regex re = new Regex("<[^>]*>");
content = re.Replace(content, "");
content = content.Replace(" ", "").Replace(" ", "");
if (content.Length <= length
)
return content;
else
CheckLeng = "1";
return content.Substring(0, length) + "……";
}
else
{
if (content.Length <= length)
return content; int pos = 0, npos = 0, size = 0;
bool firststop = false, notr = false, noli = false;
StringBuilder sb = new StringBuilder(); while (true)
{
if (pos >= content.Length)
break;
string cur = content.Substring(pos, 1);
if (cur == "<")
{
string next = content.Substring(pos + 1, 3).ToLower();
if (next.IndexOf("p") == 0 && next.IndexOf("pre") != 0)
{
npos = content.IndexOf(">", pos) + 1;
}
else if (next.IndexOf("/p") == 0 && next.IndexOf("/pr") != 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("br") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("img") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
size += npos - pos + 1;
}
}
else if (next.IndexOf("li") == 0 || next.IndexOf("/li") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!noli && next.IndexOf("/li") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
noli = true;
}
}
}
else if (next.IndexOf("tr") == 0 || next.IndexOf("/tr") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr && next.IndexOf("/tr") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
notr = true;
}
}
}
else if (next.IndexOf("td") == 0 || next.IndexOf("/td") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr)
{
sb.Append(content.Substring(pos, npos - pos));
}
}
}
else
{
npos = content.IndexOf(">", pos) + 1;
sb.Append(content.Substring(pos, npos - pos));
}
if (npos <= pos)
npos = pos + 1;
pos = npos;
}
else
{ if (size < length)
{
string GetWord = "";
if (ASCIIEncoding.ASCII.GetBytes(cur)[0] > 65 || ASCIIEncoding.ASCII.GetBytes(cur)[0] < 122)
{ bool flag = false;
while (!flag)
{
if (pos >= content.Length)
break; string str = content.Substring(pos, 1);
GetWord = GetWord + str;
byte a = ASCIIEncoding.ASCII.GetBytes(str)[0];
if (a < 65 || a > 122)
{
flag = true;
}
else
{
pos += 1;
}
}
sb.Append(GetWord); }
else
{
sb.Append(cur);
}
//sb.Append(cur);
size++;
//sb.Append(cur);
//size++; }
else
{
if (!firststop)
{
CheckLeng = "1";
//sb.Append("……");
firststop = true;
}
}
pos++;
} } string HTML = sb.ToString();
if (NoHTML(HTML).Length != NoHTML(content).Length || CheckLeng == "1")
{
CheckLeng = "1"; HTML = HTML + "……";
}
return HTML;
}
}public static string NoHTML(string Htmlstring)
{ //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", ""); Htmlstring.Replace(">", ""); Htmlstring.Replace("\r\n", ""); Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring; }以前写的一个保持HTML分割的函数,你自己试试了。
引用 8 楼 jack15850798154 的回复:
各位大哥能给我一个例子参考一下吗? 我不明白各位所说的手动分页是什么意思啊!还请指教一下。谢谢。
手动分页就是在你文章中添加一些标识例如“111111 <#page#>111112”判断的时候发现<#page#>就分页。
是不是我在添加文章的时候都要在添加文章的时候写上 “ 一些标识例如“111111 <#page#>111112” ”啊。
晕倒。你在文章中插入<#page#> 就可以了。
这样111111 是第一页。111112就是第二页啊。你在编辑框里面添加一个按钮,点击按钮,就在编辑框插入<#page#> 就可以了。