asp 图片正则 替换,替换前检查图片是不是本地地址的方法

本文介绍了一种使用ASP和正则表达式处理图片链接的方法,实现了对特定来源图片的识别和处理,避免了不必要的盗链,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- google_ad_client = "pub-4490194096475053"; /* 内容页,300x250,第一屏 */ google_ad_slot = "3685991503"; google_ad_width = 300; google_ad_height = 250; // -->

这个图片正则先检查图片的地址,不是本地的则用本地的asp突破盗链,方便使用
直接用正则替换,但没有判断功能
Function FormatImg(content)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(script)"
Content=re.Replace(Content,"script")
re.Pattern="<img.[^>]*src(=| )(.[^>]*)>"
Content=re.replace(Content,"<img src=$2 style=""cursor: pointer"" alt=""在新窗口中打开浏览"" οnclick=""javascript:window.open(this.src);"" οnlοad=""javascript:resizepic(this)"" border=""0""/>")
set re = nothing
FormatImg = content
End Function
这段代码将内容中的图片替换成 <img src=$2 style="cursor: pointer" alt="在新窗口中打开浏览" οnclick="javascript:window.open(this.src);" οnlοad="javascript:resizepic(this)" border="0"/> 这中形式的,
我现在需要提取$2的前7个字符,用来判断是否需要被替换,如果前7=特定的字符,就不要替换,但获取$2的前7 无法。大家有什么办法支下招撒?
主要是参考了下面的代码,大家可以看下
'连接
re.Pattern = "\[url=(.[^\]]*)\](.[^\[]*)\[\/url]"
Set strMatchs=re.Execute(strContent)
For Each strMatch in strMatchs
tmpStr1=checkURL(strMatch.SubMatches(0))
tmpStr2=strMatch.SubMatches(1)
strContent=replace(strContent,strMatch.Value,"<a target=""_blank"" href="""&tmpStr1&""">"&tmpStr2&"</a>",1,-1,0)
Next
这里是正则的Matchs的说明文档
http://www.jb51.net/article/15362.htm
下面由脚本之家测试代码,大家可以做出函数
<%
content2="<img src='http://www.jb51.net/images/logo.gif' width=100 />中间一些内容<img src='http://www.kanshule.com/indeximg/logo.GIF' width=200 />"
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="<img.[^>]*src(=| )(.[^>]*)[/]?>"
set Matches=re.execute(content2)
For Each strMatch in Matches
tmpStr1=(strMatch.SubMatches(1))
tmpurl=replace(replace(tmpStr1,"'",""),"""","")
//response.write left(tmpurl,20)
if left(tmpurl,19)="http://www.jb51.net" then
picurl=tmpurl
else
picurl="http://img.jb51.net/showpic.asp?url="&tmpurl
end if
Content=replace(Content2,strMatch.Value,"<img src="&picurl&" style=""cursor: pointer"" alt=""在新窗口中打开浏览"" οnclick=""javascript:window.open(this.src);"" οnlοad=""javascript:resizepic(this)"" border=""0""/>")
Next
response.write Content
set re = nothing
%>
http://www.corange.cn/archives/2008/11/2239.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值