---
title: "带HTML代码的文章摘要截取 (种实现方法)"
author: "HelloJava1234"
date: "2012-03-16T01:09:00Z"
tags: []
slug: "html"
draft: false
lastmod: "2012-04-04T22:56:16Z"
id: 1474125
url: "https://hellojava1234.iteye.com/blog/1474125"
---

1、  
function gotTopic(str,strlen)  
if str="" then  
gotTopic=""  
exit function  
end if  
dim l,t,c, i  
str=replace(replace(replace(replace(str,"&nbsp;"," "),"&quot;",chr(34)),"&gt;","&gt;"),"&lt;","&lt;")  
l=len(str)  
t=0  
for i=1 to l  
c=Abs(Asc(Mid(str,i,1)))  
if c&gt;255 then  
t=t+2  
else  
t=t+1  
end if  
if t&gt;=strlen then  
gotTopic=left(str,i) & "…"  
exit for  
else  
gotTopic=str  
end if  
next  
gotTopic=replace(replace(replace(replace(gotTopic," ","&nbsp;"),chr(34),"&quot;"),"&gt;","&gt;"),"&lt;","&lt;")  
end function

2、  
比如储存图片内容是pic,内容content

function showcontent()  
if content&lt;&gt;"" then  
response.write left(content,50)  
elseif pic&lt;&gt;"" and content="" then  
response.write "&lt;img&gt;"&"&pic&"&"&lt;/img&gt;"  
elseif pic="" and content=""then  
response.write "无内容"  
end if  
end function

3、  
'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
'切割内容 - 按行分割(行数)  
'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
Function SplitLines(byVal Content,byVal ContentNums)  
Dim ts,i,l  
ContentNums=int(ContentNums)  
If IsNull(Content) Then Exit Function  
i=1  
ts = 0  
For i=1 to Len(Content)  
l=Lcase(Mid(Content,i,5))  
If l="&lt;br/&gt;" Then  
ts=ts+1  
End If  
l=Lcase(Mid(Content,i,4))  
If l="&lt;br&gt;" Then  
ts=ts+1  
End If  
l=Lcase(Mid(Content,i,3))  
If l="&lt;p&gt;" Then  
ts=ts+1  
End If  
If ts&gt;ContentNums Then Exit For  
Next  
If ts&gt;ContentNums Then  
Content=Left(Content,i-1)  
End If  
SplitLines=Content  
End Function

'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
'切割内容 - 按字符分割（字符）  
'\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
Function CutStr(byVal Str,byVal StrLen)  
Dim l,t,c,i  
If IsNull(Str) Then CutStr="":Exit Function  
l=Len(str)  
StrLen=int(StrLen)  
t=0  
For i=1 To l  
c=Asc(Mid(str,i,1))  
If c&lt;0 Or c&gt;255 Then t=t+2 Else t=t+1  
IF t&gt;=StrLen Then  
CutStr=left(Str,i)&"..."  
Exit For  
Else  
CutStr=Str  
End If  
Next  
End Function

4、  
截取文章摘要（无损HTML）  
发表于217 天前 ? ASP ? 暂无评论

鲁莽截取：页面变形  
以前曾经做过一个班级主页的ASP系统，在首页显示文章时我用了截取措施。但是那只是鲁莽地用left(\[string],\[length])截取了一下，结果经常因为HTML标记不完全而导致页面变形。

str=left(str,500)

除去HTML元素再截取：太过于简陋  
后来想到一个很BT的方法，除去文章的所有HTML元素再截取。效果好了很多，从此再没有出现页面变形的事故。这个方法一直被我使用，从新版的班级主页到SuoMBlog到班级信息平台，全是这东西。但是当我看到百度、网易的无损HTML的截取后，突然发觉自己的代码多么丑陋。

str=left(nohtml(str),500)  
'除去HTML元素  
Function nohtml(str)  
dim re  
Set re=new RegExp  
re.IgnoreCase =true  
re.Global=True  
re.Pattern="&lt;(.\[^&gt;]\*)&gt;"  
str=re.replace(str,"")  
nohtml=str  
set re=nothing  
End Function

自动补全HTML元素：嗯，不错,完美  
其实想法很早就有了，尤其是今年年初受到HTML特性的启发，但是当时的算法太繁琐，而且效率低，自己也没心思去弄。于是就搁置在那里。今天凌晨睡不着，想到后重新构建了算法，并且在脑子里“试运行”了几遍，优化了一下后就把程序敲出来了。觉得也没有想象中的那么难，轻轻松松就解决了。所以，程序员一定不能懒。  
在这个算法里，运用了栈，因为HTML元素有始必有终嘛。看看代码就晓得咯。

str=HTMLCutter(str,500)  
'无损截取HTML  
Function HTMLCutter(str, l)  
'声明一个栈，100层够大吧  
Dim sFIFO(100)  
'一些参数  
p = 1  
str = Trim(str)  
maxlong = Len(str)  
'参数  
FlagHTML = False  
longStr = 0  
'开始扫描文章  
For i = 1 To maxlong  
'提取第i位的字符  
c = Mid(str, i, 1)  
'判断HTML元素开始  
If c = "&lt;" Then  
If FlagHTML Then longStr = longStr + i - istart  
FlagHTML = True  
istart = i  
Else  
If FlagHTML Then  
If c = "&gt;" Then  
iend = i  
tmpStr = Mid(str, istart + 1, iend - istart - 1)

'检查元素为开始还是结束  
'判断是否是的标签  
If Right(tmpStr, 1) &lt;&gt; "/" Then  
If Left(tmpStr, 1) = "/" Then  
'结束标签  
If sFIFO(p) = Right(tmpStr, Len(tmpStr) - 1) Then p = p - 1

FlagHTML = False  
Else  
'开始标签  
p = p + 1

t = InStr(1, tmpStr, " ")  
If t &lt;&gt; 0 Then tmpStr = Left(tmpStr, t - 1)

sFIFO(p) = tmpStr  
End If  
End If  
End If  
Else  
longStr = longStr + 1  
If longStr &gt;= l Then Exit For  
End If  
End If  
Next

fStr = Left(str, i)  
'把没有闭合的HTML元素补上  
For j = p To 1 Step -1  
If sFIFO(j) &lt;&gt; "" Then endStr = endStr & " Next

HTMLCutter = fStr & endStr  
End Function

5、  
给你一个我自己总结的一个函数吧，支持中英文混搭的，在文字排版上非常好用的

Function s\_len(str1,num\_n,sort\_d)  
'str1为字符串,num\_n为要显示的字数,sort\_d为结尾的符号如...或~

'str1=RecovertHTML(str1)  
str1=replace(str1, "&lt;","&lt;")'还原字符串  
str1=replace(str1, "&gt;", "&gt;")  
str1=replace(str1,""",chr(34))  
str1=replace(str1,"'",chr(39))  
'str1=replace(str1, "&lt;br /&gt;",vbCrLf)  
str1=replace(str1, " "," ")  
str1=replace(str1, " "," ")

length=len(str1)  
dim chinese,ii  
chinese=0  
for ii=1 to length

if asc(mid(str1,ii,1))&lt;1 or asc(mid(str1,ii,1))&gt;255 then  
chinese=chinese+1  
end if  
if chinese+ii&gt;num\_n then '超过规定的数，就退出循环  
exit for  
end if  
next  
s\_len=left(str1,ii)  
kk=right(s\_len,1)  
if asc(mid(str1,1,1))&lt;1 or asc(mid(str1,1,1))&gt;255 then '如果算完后最后一个是双字节数，则总数再减一  
iii=ii-1  
s\_len=left(str1,iii)  
end if

if chinese+ii&gt;num\_n then '如果标题大于规定的字数时后面加...  
s\_len=s\_len+sort\_d  
end if

s\_len=Replace(s\_len,"&lt;br /&gt;", " ")  
s\_len=Replace(s\_len,"&lt;br&gt;", " ")  
s\_len=Replace(s\_len,"&lt;","&lt;")  
s\_len=Replace(s\_len,"&gt;","&gt;")  
s\_len=Replace(s\_len," ", " ")  
s\_len=Replace(s\_len,chr(34),""")  
s\_len=Replace(s\_len,chr(39),"'")  
s\_len=Replace(s\_len,vbCrLf,"&lt;br /&gt;")

'response.write "字符串:" & str1 & "&lt;br&gt;"  
'response.write "s\_len(command)=" & s\_len & "&lt;br&gt;"  
'response.write "length+chinese字数为:" & length+chinese & "&lt;br&gt;"  
'response.write "chinese+ii=" & chinese+ii & "&lt;br&gt;"  
'response.write "ii=" & ii & "&lt;br&gt;"  
'response.write "其中中文" & chinese & "个，非中文" & length-chinese & "个&lt;br&gt;"  
End Function