当前位置:K88软件开发文章中心编程语言AspAsp01 → 文章内容

ASP获得代码中第一张图片地址

减小字体 增大字体 作者:佚名     来源:asp编程网  发布时间:2018-12-30 8:32:05

通常会做这样一个功能:在添加新闻的时候,新闻中有图片,我们需要将第一个图片取出来,保存到数据库中的另一个地方,主要是因为这个图片要用到一个独立的地方,如幻灯片或者是显示到首页。这种做法的原理是使用正则去取。
<
%
'code要检测的代码
'http://www.knowsky.com/asp.asp
'leixing html 或者ubb
'nopic 代码没有图片时默认值
function toppic(code,leixing,nopic)
 
 
 
 
 
 
 
set regex = new regexp
 
 
 
 
 
 
 
regex.ignorecase = true
 
 
 
 
 
 
 
regex.global = true
 
 
 
 
 
 
 
if leixing = "html" then
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
regex.pattern = "<
img(.*?)src\s?\=\s?(\u0022?)([^\u0022\/>
]+)"
 
 
 
 
 
 
 
else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
regex.pattern = "\[img\]([^\u005B]+)“
 
 
 
 
 
 
 
end if
 
 
 
 
 
 
 
set matches = regex.execute(code)
 
 
 
 
 
 
 
if regex.test(code) then
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
if leixing = "html" then
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
toppic = matches(0).submatches(3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
toppic = matches(0).submatches(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
end if
 
 
 
 
 
 
 
else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
toppic = nopic
 
 
 
 
 
 
 
end if
end function
code1 = "<
img src=""http://www.aspbc.com/Index/showtech/id/1.gif"">
"
pic = toppic(code1,"html","nopic.gif")
response.Write pic
% >


ASP获得代码中第一张图片地址