28,391
社区成员
发帖
与我相关
我的任务
分享
'**************************************************
'函数名:gotTopic
'作 用:截字符串,汉字一个算两个字符,英文算一个字符
'参 数:str ----原字符串
' strlen ----截取长度
'返回值:截取后的字符串
'**************************************************
function gotTopic(str,strlen)
if str="" then
gotTopic=""
exit function
end if
dim l,t,c, i
str=replace(replace(replace(replace(str," "," "),""",chr(34)),">",">"),"<","<")
l=len(str)
t=0
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
gotTopic=left(str,i)
exit for
else
gotTopic=str
end if
next
gotTopic=replace(replace(replace(replace(gotTopic," "," "),chr(34),"""),">",">"),"<","<")
<%Class TLeft
Private c_Max, c_o, c_n, c_c, c_x, c_s
Private c_d, c_a, c_r
Private Sub Class_Initialize()
'c_Max 控制查找最大数
'c_o 控制是否继续查找
'c_n 记录字数
'c_c 记录未结束标记数
'c_x 无效未结束标记数
'c_s 预处理的String
'c_d 记录所有没有结尾的标记
'c_a 记录所有匹配出的内容
'c_r 公用正则对象
c_Max = 0
Set c_d = Server.CreateObject("Scripting.Dictionary")
Set c_a = Server.CreateObject("Scripting.Dictionary")
Set c_r = new RegExp
End Sub
Private Sub Class_Terminate
c_d.RemoveAll : Set c_d = Nothing
c_a.RemoveAll : Set c_a = Nothing
Set c_r = Nothing
End Sub
Private Sub Sd()
'set d
Dim m, i
c_r.Pattern = "<[^>]+\/>"
c_r.Global = True
Set m = c_r.Execute(c_s)
For i = 0 To m.Count - 1
c_d.Add i, m(i).Value
Next
c_s = c_r.Replace(c_s, "を")
End Sub
Private Function Ss()
'scan string
Dim a, i, s
s = toString(c_a) : a = Split(s, "を") : s = ""
For i = 0 To UBound(a)
s = s & a(i)
If i < UBound(a) - 1 Then s = s & c_d.Item(i)
Next
Ss = s
End Function
Private Function toString(o)
'dic toString
Dim a, i, s
a = o.Keys
For i = 0 To o.Count - 1
s = s & o.Item(a(i))
Next
toString = s
End Function
Private Sub Exec(a, b, i)
If a <> "" Then
If c_n < c_Max Then
If a <> "を" Then c_n = c_n + 1
c_a.Add i, a
End If
Else
If Instr(b, "</") = 1 Then
If c_n < c_Max Then
c_a.Add i, b
ElseIf c_x = 0 And c_c > 0 Then
c_a.Add i, b
If c_c = 1 Then c_o = False
Else
c_x = c_x - 1
End If
c_c = c_c - 1
Else
If c_n < c_Max Then
c_a.Add i, b
Else
c_x = c_x + 1
End If
c_c = c_c + 1
End If
End If
End Sub
Private Sub Start()
Dim m, i
Call Sd
c_r.Pattern = "(<[^>]+>)|([\S\s])"
c_r.Global = True
Set m = c_r.Execute(c_s)
For i = 0 To m.Count - 1
If c_o = False Then Exit For
Exec m(i).SubMatches(1), m(i).SubMatches(0), i
Next
End Sub
Public Property Get Parse(s, n)
'return String
c_o = True : c_Max = n : c_n = 0 : c_c = 0 : c_x = 0 : c_s = s
c_a.RemoveAll : c_d.RemoveAll
Call Start
Parse = Ss
End Property
End Class
Dim wc, strng : strng = "<font color=""red"" size=""2""><strong><img src=""csdn"" />String</strong>" _
& "<b><img src=""csdn"" />String</b></font><div></div>"
Set wc = new TLeft
With Response
.Write Server.HTMLEncode(wc.Parse(strng, 1))
.Write "<hr />"
.Write Server.HTMLEncode(wc.Parse(strng, 6))
.Write "<hr />"
.Write Server.HTMLEncode(wc.Parse(strng, 7))
End With
Set wc = Nothing
%>
一个高人写的“截取含html的字符串”
'//--清除html代码--//
Function clearHtml(fstr)
Set regEx = New RegExp
regEx.Pattern = "<\/?[^>]*>"
regEx.IgnoreCase = true
regEx.Global = true
set re = regEx.execute(fstr)
fstr = regEx.Replace(fstr,"")
fstr = Replace(fstr," "," ")
clearHtml = fstr
Set reg=Nothing
End Function
'//调用这个函数,清除所有的html代码之后再对字符串进行截取