含HTML标记的内容分页

 

最近网站要做静态生成内容必须分页所以在网上找了N多实例都不理想,所以花了点时间自己写了个方法

目前来说没发现什么问题(已用方法生成20W以上的html)

所以把代码贴出来与大家分享。

不足之处或有更好的方法请大家告知我不胜感激。

 

调用ArrayList arrlt=ContentPage.GetPageContent("分页内容", 分页大小,true);

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 内容分页 v0.1.2
    
/// http://cn795.cnblogs.com
    
/// </summary>

    public class ContentPage
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 内容分页
        
/// </summary>
        
/// <param name="strContent">要分页的字符串内容</param>
        
/// <param name="intPageSize">分页大小</param>
        
/// <param name="isOpen">最后一页字符小于intPageSize的1/4加到上一页</param>
        
/// <returns></returns>

        public static ArrayList GetPageContent(string strContent, int intPageSize, bool isOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ArrayList arrlist 
= new ArrayList();
            
string strp = strContent;
            
int num = RemoveHtml(strp.ToString()).Length;//除html标记后的字符长度
            int bp = (intPageSize + (intPageSize / 5));

            
for (int i = 0; i < ((num % bp == 0? (num / bp) : ((num / bp) + 1)); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                arrlist.Add(SubString(intPageSize, 
ref strp));
                num 
= RemoveHtml(strp.ToString()).Length;
                
if (isOpen && num < (intPageSize / 4))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
// 小于分页1/4字符加到上一页
                    arrlist[arrlist.Count - 1= arrlist[arrlist.Count - 1+ strp;
                    strp 
= "";
                }

                i 
= 0;
            }

            
if (strp.Length > 0) arrlist.Add(strp);  //大于1/4字符 小于intPageSize 

            
return arrlist;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// &lt; 符号搜索
        
/// </summary>
        
/// <param name="cr"></param>
        
/// <returns></returns>

        private static bool IsBegin(char cr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return cr.Equals('<');
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// &gt;  符号搜索
        
/// </summary>
        
/// <param name="cr"></param>
        
/// <returns></returns>

        private static bool IsEnd(char cr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return cr.Equals('>');
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 截取分页内容
        
/// </summary>
        
/// <param name="index">每页字符长度</param>
        
/// <param name="str"></param>
        
/// <returns></returns>

        private static string SubString(int index, ref string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ArrayList arrlistB 
= new ArrayList();
            ArrayList arrlistE 
= new ArrayList();
            
string strTag = "";
            
char strend = '0';
            
bool isBg = false;
            
bool IsSuEndTag = false;

            index 
= Gindex(str, index);
            
string substr = CutString(str, 0, index);  //截取分页长度
            string substr1 = CutString(str, index, str.Length - substr.Length); //剩余字符
            int iof = substr.LastIndexOf("<"), iof1 = 0;

ContractedSubBlock.gifExpandedSubBlockStart.gif            
防止标记截断#region 防止标记截断

            
if (iof > 0) iof1 = CutString(substr, iof, substr.Length - iof).IndexOf(">"); // 标记是否被截断
            if (iof1 < 0//完整标记被截断,则重新截取
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                index 
= index + substr1.IndexOf(">"+ 1;
                substr 
= CutString(str, 0, index);
                substr1 
= CutString(str, index, str.Length - substr.Length);
            }


            
int indexendtb = substr.LastIndexOf("</tr>");
            
if (indexendtb >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                substr 
= CutString(str, 0, indexendtb);
                substr1 
= CutString(str, indexendtb, str.Length - indexendtb);
            }


            
int intsubstr = substr.LastIndexOf("/>"+ 1;
            
int intsubstr1 = substr1.IndexOf("</");
            
if (intsubstr >= 0 && intsubstr1 >= 0)   // <xx /> 标记与  </xx>结束标记间是否字符  如:<a href="#"><img src="abc.jpg" />文字文字文字文字</a>
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                
string substr2 = CutString(substr, intsubstr, substr.Length - intsubstr) + CutString(substr1, 0, intsubstr1);
                
if (substr2.IndexOf('>'== -1 && substr2.IndexOf('<'== -1)   // 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    substr 
+= CutString(substr1, 0, intsubstr1);
                    substr2 
= CutString(substr1, intsubstr1, substr1.Length - intsubstr1);
                    
int sub2idf = substr2.IndexOf('>');
                    substr 
+= CutString(substr2, 0, sub2idf);
                    substr1 
= CutString(substr2, sub2idf, substr2.Length - sub2idf);
                }

            }

            
#endregion


            
//分析截取字符内容提取标记
            foreach (char cr in substr)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (IsBegin(cr)) isBg = true;
                
if (isBg) strTag += cr;

                
if (isBg && cr.Equals('/'&& strend.Equals('<')) IsSuEndTag = true;

                
if (IsEnd(cr))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (strend.Equals('/')) //跳出 <XX />标记
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    {
                        isBg 
= false;
                        IsSuEndTag 
= false;
                        strTag 
= "";
                    }


                    
if (isBg)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (!CutString(strTag.ToLower(), 03).Equals("<br"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
if (IsSuEndTag)
                                arrlistE.Add(strTag);  
//结束标记
                            else
                                arrlistB.Add(strTag);  
//开始标记
                        }

                        IsSuEndTag 
= false;
                        strTag 
= "";
                        isBg 
= false;
                    }

                }

                strend 
= cr;
            }


            
//找到未关闭标记
            for (int b = 0; b < arrlistB.Count; b++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
for (int e = 0; e < arrlistE.Count; e++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
string strb = arrlistB[b].ToString().ToLower();
                    
int num = strb.IndexOf(' ');
                    
if (num > 0) strb = CutString(strb, 0, num) + ">";
                    
if (strb.ToLower().Replace("<""</").Equals(arrlistE[e].ToString().ToLower()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        arrlistB.RemoveAt(b);
                        arrlistE.RemoveAt(e);
                        b 
= -1;
                        
break;
                    }

                }

            }


            
//关闭被截断标记
            for (int i = arrlistB.Count; i > 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
string stral = arrlistB[i - 1].ToString();
                substr 
+= (stral.IndexOf(" "== -1 ? stral.Replace("<""</") : CutString(stral, 0, stral.IndexOf(" ")).Replace("<""</"+ ">");
            }

            
//补全上页截断的标签
            string strtag = "";
            
for (int i = 0; i < arrlistB.Count; i++) strtag += arrlistB[i].ToString();

            str 
= strtag + substr1; //更改原始字符串
            return substr; //返回截取内容
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回真实字符长度
         
/// </summary>
        
/// <param name="str"></param>
        
/// <param name="index"></param>
        
/// <returns></returns>

        private static int Gindex(string str, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
bool isBg = false;
            
bool isSuEndTag = false;
            
bool isNbsp = false,isRnbsp=false;;
            
string strnbsp="";
            
int i = 0, c = 0;
            
foreach (char cr in str)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (!isBg && IsBegin(cr)) { isBg = true; isSuEndTag = false; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (isBg && IsEnd(cr)) { isBg = false; isSuEndTag = true; }
               
                
if (isSuEndTag && !isBg) //不在html标记内
ExpandedSubBlockStart.gifContractedSubBlock.gif
                
                    
if (cr.Equals('&')) isNbsp = true;
                    
if (isNbsp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        strnbsp 
+= cr.ToString();
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if (strnbsp.Length > "&nbsp;".Length) { isNbsp = false; strnbsp = ""; }
                        
if (cr.Equals(';')) isNbsp = false;//
                    }

                    
if (!isNbsp && !"".Equals(strnbsp)) isRnbsp = strnbsp.ToLower().Equals("&nbsp;");
                }


ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ((isSuEndTag || (!isBg && !isSuEndTag)) && !cr.Equals('\n'&& !cr.Equals('\r'&& !cr.Equals(' ')) { c++; }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (isRnbsp) { c = c - 6; isRnbsp = false; strnbsp = ""; }

                i
++;

                
if (c == index) return i;
            }

            
return i;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 移除Html标记
        
/// </summary>
        
/// <param name="content"></param>
        
/// <returns></returns>

        public static string RemoveHtml(string content)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
             content
=Regex.Replace(content, @"<[^>]*>"string.Empty, RegexOptions.IgnoreCase);
             
return Regex.Replace(content, "&nbsp;"string.Empty, RegexOptions.IgnoreCase);
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 从字符串的指定位置截取指定长度的子字符串
        
/// </summary>
        
/// <param name="str">原字符串</param>
        
/// <param name="startIndex">子字符串的起始位置</param>
        
/// <param name="length">子字符串的长度</param>
        
/// <returns>子字符串</returns>

        public static string CutString(string str, int startIndex, int length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (startIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (length < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    length 
= length * -1;
                    
if (startIndex - length < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        length 
= startIndex;
                        startIndex 
= 0;
                    }

                   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        startIndex 
= startIndex - length;
                    }

                }


                
if (startIndex > str.Length) return "";

            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (length < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return "";
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (length + startIndex > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
{
                        length 
= length + startIndex;
                        startIndex 
= 0;
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
return "";
                    }

                }

            }


            
if (str.Length - startIndex < length) length = str.Length - startIndex;

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return str.Substring(startIndex, length);
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return str;
            }

        }

    }

}

代码下载:ContentPage.cs.txt

转载请注明出处:http://www.cnblogs.com/cn795 

转载于:https://www.cnblogs.com/cn795/archive/2009/03/20/1417992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML中的`<ul>`标签用来创建无序列表,它并没有直接支持分页的功能。如果你想要在页面中实现分页效果,可以考虑以下几种方法: 1. 使用CSS样式控制。你可以给`<ul>`标签添加`height`和`overflow`属性,控制列表的高度和滚动条的显示,从而实现分页的效果。例如: ``` ul { height: 200px; overflow: auto; } ``` 2. 使用JavaScript实现。你可以通过JavaScript来获取列表的子元素数量,然后按照每页显示的数量来分割列表,并动态创建分页控件。例如: ``` var list = document.querySelector('ul'); var itemsPerPage = 10; var totalPages = Math.ceil(list.children.length / itemsPerPage); function showPage(page) { var startIndex = (page - 1) * itemsPerPage; var endIndex = startIndex + itemsPerPage - 1; for (var i = 0; i < list.children.length; i++) { if (i >= startIndex && i <= endIndex) { list.children[i].style.display = 'block'; } else { list.children[i].style.display = 'none'; } } } // 创建分页控件 var pagination = document.createElement('div'); for (var i = 1; i <= totalPages; i++) { var pageLink = document.createElement('a'); pageLink.textContent = i; pageLink.href = '#'; pageLink.addEventListener('click', function(e) { e.preventDefault(); showPage(parseInt(this.textContent)); }); pagination.appendChild(pageLink); } document.body.appendChild(pagination); // 显示第一页 showPage(1); ``` 这段代码会将列表分成每页10条数据,并动态创建分页控件。每次点击分页控件上的页码,就会显示对应的页数。 希望这些方法对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值