asp中,使用正则提取文章中第一张图片路径并显示
上次我发过个【asp中,如何在首页显示新闻内容中的第一张图片】,纠结的一个事情出现了,我将此调用的数据单独放一个页面测试时,是可以读取的【...<img border="0" src=<%=RegExp_Execute(regstr,rs("content"))%> width="200" height="200" />...】。然后整体放网站去时,这个图片就读不出来了。
附: Function RegExp_Execute(patrn, strng)
Dim regEx, Match, Matches,values
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = true
regEx.Global = True
For Each Match in Matches
values=Match.Value
Next
RegExp_Execute = values
End Function
[解决办法]
你发的这个代码。根本就不是提取图片的。
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 FunctionFunction 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'把里面的地址串起来备用 NextEnd Function