基于行数预测的长文章分页
说句老实话,我是个不大习惯写博的主,而且本次写博目的是想过来发个小广告,哈哈,都是园子里的兄弟不要拿砖头扔我
园子里的兄弟时间都是很宝贵的.为了大家消消火,少让大家过来扔砖头.所以就把最近项目里碰到的一个长文章自动分页的问题跟大家分享下.说起长文章分页也是属于老生常谈了,网上搜下可以找到一箩筐。目前网上流传的代码大多是基于对文章中字符的多少来进行,这种方法对于图片等这些标记就束手无策了。
由于目前文章上传大多采用HTML编辑器,使得里面参杂的HTML代码严重影响到场文章分页的效果。我现在想说的是一种基于文章行数预测为思路的一种分页方式.操作过程也很简单.
大家都是写程序的,费话少说,看代码
1
//======================================================
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
// 50M 双线空间 + 10M数据库 + 50M邮箱 18元/年
4
// 100M 双线空间 + 50M数据库 + 100M邮箱 28元/年
5
// 200M 双线空间 + 50M数据库 + 100M邮箱 38元/年
6
// 特价主机,有需要的M我 QQ:70975363
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
//======================================================
9
10
private static List<string> HtmlLineTest(ref string content, int pageid, string url)
11
{
12
//文章分段数组
13
List<string> segs = new List<string>();
14
int segmax = 30; // 计划每一面的行数
15
int linewords = 50;
16
int segmin = 0;
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
string s;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
//int pageheight = 25;
21
int line = 0;
22
对含有HTML代码的文章进行整理
28
MatchCollection mcc = Regex.Matches(content, "<br */*>|<img[^<>]+>", RegexOptions.IgnoreCase);
29
int idx = 0;
30
StringBuilder __article = new StringBuilder();
31
foreach (Match m in mcc)
32
{
33
if (m.Value.StartsWith("<br", StringComparison.OrdinalIgnoreCase))
34
{
35
s = content.Substring(idx, m.Index - idx);
36
if (!s.StartsWith(" "))
37
__article.Append(" ");
38
__article.Append(s);
39
idx = m.Index + m.Value.Length;
40
int lth = s.Length / linewords;
41
if (s.Length % linewords < linewords/2)
42
lth++;
43
line += lth == 0 ? 1 : lth;
44
segmin += lth == 0 ? 1 : lth;
45
__article.Append("<br />");
46
if (segmax <= segmin)
47
{
48
segs.Add(__article.ToString());
49
_string.Remove(0, __article.Length);
50
segmin = 0;
51
}
52
}
53
else if (m.Value.StartsWith("<img", StringComparison.OrdinalIgnoreCase))
54
{
55
int pos = m.Value.IndexOf("height");
56
if (pos > -1)
57
{
58
59
string ms = m.Value.Substring(pos + 6);
60
pos = int.Parse(Regex.Match(ms, "\\d+").Value);
61
line += pos / 25;
62
segmin += pos / 25;
63
}
64
else
65
{
66
// 图片没有发现表示高度的属性
67
line += 400 / 25;
68
segmin += 400 / 25;
69
}
70
idx = m.Index + m.Value.Length;
71
__article.Append(m.Value);
72
__article.Append("<br />");
73
if (segmax <= segmin)
74
{
75
segs.Add(_string.ToString());
76
__article.Remove(0, _string.Length);
77
segmin = 0;
78
}
79
}
80
}
81
if(__article.Length>0)
82
segs.Add(_string.ToString());
83
if (mcc.Count == 0)
84
segs.Add(content);
85
return segs;
86
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
当然了.这是服务端的执行代码.为了能够有效检测图片的长宽,我们把这些工作放在客户端来完成
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
// ++++++++++++++++++++++++++
3
// 这段代码是针对fckeditor编码器出+ JQuery 写的
4
//++++++++++++++++++++++++++
5
function ImgStyleTest()
6
{
7
var FckDocument = $('iframe').eq(1)[0].contentWindow.document;
8
var HtmlContainer = FckDocument.getElementsByTagName("iframe")[0].contentWindow.document;
9
var imgs = HtmlContainer.getElementsByTagName("img");
10
for (i=0; i<imgs.length;i++ )
11
{
12
var imgObj = new Image();
13
imgObj.src = imgs[i].src;
14
// 检测图片有没有加载完成
15
if(imgObj.complete) {
16
var width = imgObj.width;
17
var height = imgObj.height;
18
// 对长度超过600的图片进行缩放
19
if(width>600) {
20
height = 600 * height / width;
21
width = 600;
22
}
23
imgs[i].style.width= width;
24
imgs[i].style.height= height;
25
} else {
26
setTimeout("ff()",1000);
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
}
30
}
31
//======================================================
32
// 50M 双线空间 + 10M数据库 + 50M邮箱 18元/年
33
// 100M 双线空间 + 50M数据库 + 100M邮箱 28元/年
34
// 200M 双线空间 + 50M数据库 + 100M邮箱 38元/年
35
// 300M 双线空间 + 50M数据库 + 100M邮箱 48元/年
36
// 600M 双线空间 + 100M数据库 + 200M邮箱 80元/年
37
// 1G 双线空间 + 200M数据库 + 300M邮箱 100元/年
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
// 特价主机,有需要的M我 QQ:70975363
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
//======================================================
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这种做法是针对图片和文章的段进行文章分页的.如果某些文章有些段特长那就无能为力了,但是可以结合长段用字段
长度分割相结合的方式进行分页.