把下面代码保存成 get_intro.asp 传到根目录。运行后删除。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="Conn.asp"-->
<%'批量取内容的前N个字符做导读(前提是导读为空。如果想替换所有,请把红色部分删除就可以了。
dim strlen '截取的字符数
strlen=200 '默认200个字符(100个汉字)
sql="select Intro,ArticleContent from KS_Article where rtrim(Intro)='' or isnull(Intro)"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
if not (rs.bof and rs.eof) then
do while not rs.eof
rs(0)=GotTopic(LoseHtml(replace(rs(1),"[NextPage]","")),strlen)
rs.update
rs.movenext
loop
response.write "共批量替换了"&rs.recordcount&"条记录"
else
response.write "没有可替换的内容"
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
Function LoseHtml(ContentStr)
On Error Resume Next
Dim TempLoseStr, regEx
If ContentStr="" Or ContentStr=Null Then Exit Function
TempLoseStr = CStr(ContentStr)
Set regEx = New RegExp
regEx.Pattern = "<\/*[^<>]*>"
regEx.IgnoreCase = True
regEx.Global = True
TempLoseStr = regEx.Replace(TempLoseStr, "")
LoseHtml = TempLoseStr
End Function
'*************************************************************************
'函数名:gotTopic
'作 用:截字符串,汉字一个算两个字符,英文算一个字符
'参 数:str ----原字符串
' strlen ----截取长度
'返回值:截取后的字符串
'*************************************************************************
Function GotTopic(ByVal Str, ByVal strlen)
If Str = "" OR IsNull(Str) Then GotTopic = "":Exit Function
If strlen=0 Then GotTopic=Str:Exit Function
Dim l, T, c, I, strTemp
Str = Replace(Replace(Replace(Replace(Str, " ", " "), """, Chr(34)), ">", ">"), "<", "<")
l = Len(Str)
T = 0
strTemp = Str
strlen = CLng(strlen)
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
strTemp = Left(Str, I)
Exit For
End If
Next
If strTemp <> Str Then strTemp = strTemp
GotTopic = Replace(Replace(Replace(Replace(strTemp, " ", " "), Chr(34), """), ">", ">"), "<", "<")
End Function%>