使用ASP来获取文章内容中第一张图片源代码

[ 2010-03-31 14:21:23 | 作者: admin ]
字号: | |
用asp做网站,有时候会需要从内容中提取第一张图片,这段代码就可以实现,完全无错
运行结果:
http://life.52News.com/skins/2009/images/logo.gif
引用
处理内容开始
<IMG alt="" src="http://life.52News.com/skins/2009/images/logo.gif">本站的宣传图片
处理内容结束
引用
<%

if GetFirstImg(str)="" then
response.write "未找到图片"
else
response.write GetFirstImg(str)
end if

function GetFirstImg(str) '取得img 标签内容
        dim tmp
        Set objRegExp = New Regexp
          objRegExp.IgnoreCase = True '忽略大小写
          objRegExp.Global = false '全文搜索 !关键!
          objRegExp.Pattern = "<img (.*?)src=(.[^\[^>]*)(.*?)>"
        Set Matches =objRegExp.Execute(str)
        For Each Match in Matches
                 tmp=tmp & Match.Value
        Next
          GetFirstImg=GetImgS(tmp)
end function

function GetImgS(str)'获取所有图片
        Set objRegExp1 = New Regexp
          objRegExp1.IgnoreCase = True '忽略大小写
          objRegExp1.Global = True '全文搜索
        objRegExp1.Pattern = "src\=.+?\.(gif|jpg|png|bmp)"
        set mm=objRegExp1.Execute(str)
        For Each Match1 in mm
                 imgsrc=Match1.Value
               '也许存在不能过滤的字符,确保万一
                 imgsrc=replace(imgsrc,"""","")
                 imgsrc=replace(imgsrc,"src=","")
                 imgsrc=replace(imgsrc,"<","")
                 imgsrc=replace(imgsrc,">","")
                 imgsrc=replace(imgsrc,"img","")
                 imgsrc=replace(imgsrc," ","")
                 GetImgS=GetImgS&imgsrc'把里面的地址串起来备用
        next
end function
%>
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1366

这篇日志没有评论。

此日志不可发表评论。