熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP常用函數:getIMG()

2013-11-13 10:43:55  來源: .NET編程 

  目前還是有BUG的最新的測試頁面在:

  正則分析頁面在:
發現BUG請在後面留言謝謝
修正
src=後面有空格不能正確匹配已修正
src=為空時出錯已修正
發現BUG: 圖片路徑有多個空格時只能保留一個未修正
修正
圖片路徑有多個空格時只能保留一個的BUG已修正
<%
功能:獲取全部圖片地址保存到一個數組
來源
需要ReplaceAll函數

  Function getIMG(sString)
  Dim sReallyDo regEx iReallyDo
  Dim oMatches cMatch
  //定義一個空數組
  iReallyDo =
  ReDim aReallyDo(iReallyDo)
  If IsNull(sString) Then
  getIMG = aReallyDo
  Exit Function
  End If
  //格式化HTML代碼
  //將每個 <img 換行 方便正則替換
  sReallyDo = sString
  On Error Resume Next
  sReallyDo = Replace(sReallyDo vbCr " ")
  sReallyDo = Replace(sReallyDo vbLf " ")
  sReallyDo = Replace(sReallyDo vbTab " ")
  sReallyDo = Replace(sReallyDo "<img " vbCrLf & "<img " )
  sReallyDo = Replace(sReallyDo "/>" " />" )
  sReallyDo = ReplaceAll(sReallyDo "= " "=" True)
  sReallyDo = ReplaceAll(sReallyDo "> " ">" True)
  sReallyDo = Replace(sReallyDo "><" ">" & vbCrLf & "<")
  sReallyDo = Trim(sReallyDo)
  Set regEx = New RegExp
  regExIgnoreCase = True
  regExGlobal = True
  //去除onclickonload等腳本
  regExPattern = "s[on]+?=([""|])(*?)"
  sReallyDo = regExReplace(sReallyDo "")
  //將SRC不帶引號的圖片地址加上引號
  regExPattern = "<img*?ssrc=([^""s][^""s>]*)*?>"
  sReallyDo = regExReplace(sReallyDo "<img src=""$"" />")
  //正則匹配圖片SRC地址

  regExPattern = "<img*?ssrc=([""])([^""]+?)*?>"
  Set oMatches = regExExecute(sReallyDo)
  //將圖片地址存入數組
  For Each cMatch in oMatches
  iReallyDo = iReallyDo +
  ReDim PReserve aReallyDo(iReallyDo)
  aReallyDo(iReallyDo) = regExReplace(cMatchValue "$")
  Next
  getIMG = aReallyDo
End Function
%>

  <%
用法:很多人都問我怎麼用其實數組怎麼用的這個就怎麼用
sContent = "HTML代碼字段" //sContent代表Html代碼原來寫個ors怎麼就不懂是記錄集呢
Dim aImages : aImages = GetImg(sContent) //定義一個數組並且用來存放所有分析到的圖片地址
列出所有圖片的地址:
For i = To UBound(aImages)
  ResponseWrite("<b>第" & i + & "張圖片地址:</b> " & aImages(i) & "<br />")
Next
列出第一張圖片地址:
If UBound(aImages)> Then ResponseWrite("<p><b>第一張圖片地址:</b> " & aImages() & "</p>")
列出最後一張圖片地址:
If UBound(aImages)> Then ResponseWrite("<p><b>最後一張圖片地址:</b> " & aImages(UBound(aImages)) & "</p>")
%>


From:http://tw.wingwit.com/Article/program/net/201311/14392.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.