有时候我们内容里面有多个图片路径,我们工作需要可能只想用第一张图片或者获取所有图片的名称存到数据库,下面青岛星网跟大家分享:ASP获取HTML内容里的第一张图片与所有图片名称的方法。
ASP获取HTML内容里的第一张图片与所有图片的代码如下
'取得第一个img标签内容的函数 Function GetFirstImg(Str) 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 '调用方法 htmlBody="<img id='img' src='/images/01.jpg' alt='图片标题' style='border:none;position:relative;' /><img src='/111.jpg' /><img src='/222.jpg' />" Response.Write GetFirstImg(htmlBody)