精美而实用的网站,关注web编程技术与网站SEO推广,追求极致,让您轻松愉快的学习

做手机网站的时候图片要做自适应,由于手机网站跟PC网站共用1个数据库,导致文章内的图片有部分带有:style=""等属性,导致手机端图片width:100%失效,去掉mg标签style属性只保留src即可解决问题。

ASP去除img标签style属性只保留src

'功能:将IMG代码格式化为<img src="XXX" />格式,只保留SRC属性,去掉IMG标签的其他属性。

Function FixImg(sString)
 
    Dim sReallyDo, regEx, iReallyDo
    Dim oMatches, cMatch
    Dim tStartTime, tEndTime
    If IsNull(sString) Then
        FixImg = ""
        Exit Function
    End If
    sReallyDo = sString
    On Error Resume Next
    sReallyDo = Replace(sReallyDo, vbCr, " ")
    sReallyDo = Replace(sReallyDo, vbLf, " ")
    sReallyDo = Replace(sReallyDo, vbTab, " ")
    sReallyDo = Replace(sReallyDo, "<img ", vbCrLf & "<img ", 1, -1, 1)
    sReallyDo = Replace(sReallyDo, "/>", " />", 1, -1, 1)
    sReallyDo = ReplaceAll(sReallyDo, "= ", "=", True)
    sReallyDo = ReplaceAll(sReallyDo, "> ", ">", True)
    sReallyDo = Replace(sReallyDo, "><", ">" & vbCrLf & "<")
    sReallyDo = Trim(sReallyDo)
    On Error GoTo 0
    Set regEx = New RegExp
    regEx.IgnoreCase = True
    regEx.Global = True
    '//去除onclick,onload等脚本
    regEx.Pattern = "\s[on].+?=([\""|\'])(.*?)\1"
    sReallyDo = regEx.Replace(sReallyDo, "")
    '//将SRC不带引号的图片地址加上引号
    regEx.Pattern = "<img.*?\ssrc=([^\""\'\s][^\""\'\s>]*).*?>"
    sReallyDo = regEx.Replace(sReallyDo, "<img src=""$1"" />")
    '//正则匹配图片SRC地址
    regEx.Pattern = "<img.*?\ssrc=([\""\'])([^\""\']+?)\1.*?>"
    sReallyDo = regEx.Replace(sReallyDo, "<img src=""$2"" />")
    FixImg = sReallyDo
End Function
Tags:img标签 ASP
如果本文帮到了您,请分享到朋友圈或推荐给好友!

网络编程 - 分类栏目