当前位置:编程学习 > asp >>

asp提取文章内容里面第一张图片的函数代码

'===========================================
'过程名:GetImg
'作 用:取得文章中第一张图片
'作 者:www.zzzyk.com
'参 数:str ------ 文章内容
'参 数:strpath ------ 保存图片的路径
'===========================================
Function GetImg(str,strpath)
set objregEx = new RegExp
objregEx.IgnoreCase = true
objregEx.Global = true
zzstr=""&strpath&"(.+?)\.(jpg|gif|png|bmp)"
objregEx.Pattern = zzstr
set matches = objregEx.execute(str)
for each match in matches
retstr = retstr &"|"& Match.Value
next
if retstr<>"" then
Imglist=split(retstr,"|")
Imgone=replace(Imglist(1),strpath,"")
GetImg=Imgone
else
GetImg=""
end if
end function

但是它只能取 后缀 不能取html里的<img>所以我们需要加上正侧,同时这个函数包含了提取全部图片的代码


Function GetImg(str)
dim objregEx,zzstr,matches,Match,retstr,Imglist,Imgone,strpath
set objregEx = new RegExp
objregEx.IgnoreCase = true
objregEx.Global = true
zzstr="<img(.+?)\.(jpg|gif|png|bmp)(.+?)>"
objregEx.Pattern = zzstr
set matches = objregEx.execute(str)
for each match in matches
retstr = retstr &"|"& Match.Value
next
if retstr<>"" then
Imglist=split(retstr,"|")
Imgone=replace(Imglist(1),strpath,"")
GetImg=Imgone
else
GetImg=""
end if
end function

调用的时候 只需要 <%=GetImg(rs("字段"))%>

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,