用正则表达式取文章里的图片地址
从一段文字中获取第一张图片的地址
参考网上各种正则,终于调试出来了,呵呵。
<%
'str="<a href=""post.php?action=newthread&fid=2&extra=page%3D1""><img alt='123' src=""http://127.0.0.1/images/blue/newtopic.gif"" border=""0"" alt="""" /></a><img src=""/customavatars/user.gif"" vspace=""5""><br /><img src=""/customavatars/user.gif"" vspace=""5""><img src=""images/blue/top.gif"" border=""0"" alt=""顶部"" />"
str="<img src=images/200610261533.jpg><img src=""images/200609151656.jpg""><img src=images/200609051735.JPG>"
response.write(GetImgSrc(str))
function GetImgSrc(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
GetImgSrc=getimgs(tmp)
end function
function getimgs(str)'取得
Set objRegExp1 = New Regexp
objRegExp1.IgnoreCase = True '忽略大小写
objRegExp1.Global = True '全文搜索
objRegExp1.Pattern = "src=.+[(gif|jpg|png)]+" '表达式
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
%>
'str="<a href=""post.php?action=newthread&fid=2&extra=page%3D1""><img alt='123' src=""http://127.0.0.1/images/blue/newtopic.gif"" border=""0"" alt="""" /></a><img src=""/customavatars/user.gif"" vspace=""5""><br /><img src=""/customavatars/user.gif"" vspace=""5""><img src=""images/blue/top.gif"" border=""0"" alt=""顶部"" />"
str="<img src=images/200610261533.jpg><img src=""images/200609151656.jpg""><img src=images/200609051735.JPG>"
response.write(GetImgSrc(str))
function GetImgSrc(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
GetImgSrc=getimgs(tmp)
end function
function getimgs(str)'取得
Set objRegExp1 = New Regexp
objRegExp1.IgnoreCase = True '忽略大小写
objRegExp1.Global = True '全文搜索
objRegExp1.Pattern = "src=.+[(gif|jpg|png)]+" '表达式
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
%>
输出:
images/200610261533.jpg