程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程問題解答 >> 如何把URL和郵件地址轉換為超級鏈接?

如何把URL和郵件地址轉換為超級鏈接?

編輯:編程問題解答

Function InsertHyperlinks(inText)
Dim objRegExp, strBuf
Dim objMatches, objMatch
Dim Value, ReplaceValue, iStart, iEnd

  strBuf = ""
  iStart = 1
  iEnd = 1
  Set objRegExp = New RegExp

  objRegExp.Pattern = "\b(www|http|\S+@)\S+\b" 

' 判斷URLs和emails.
  objRegExp.IgnoreCase = True                 

' 設置大小寫不敏感..
  objRegExp.Global = True                     

' 全局適用.
  Set objMatches = objRegExp.Execute(inText)
  For Each objMatch in objMatches
    iEnd = objMatch.FirstIndex
    strBuf = strBuf & Mid(inText, iStart, iEnd-iStart+1)
    If InStr(1, objMatch.Value, "@") Then
      strBuf = strBuf & GetHref(objMatch.Value, "EMAIL", "_BLANK")
    Else
      strBuf = strBuf & GetHref(objMatch.Value, "WEB", "_BLANK")
    End If
    iStart = iEnd+objMatch.Length+1
  Next
  strBuf = strBuf & Mid(inText, iStart)
  InsertHyperlinks = strBuf
End Function

Function GetHref(url, urlType, Target)
Dim strBuf

  strBuf = "<a href="""
  If UCase(urlType) = "WEB" Then
    If LCase(Left(url, 3)) = "www" Then
      strBuf = "<a href=""URL:" & url & """超級鏈接:""" & _
              Target & """>" & url & "</a>"
    Else
      strBuf = "<a href=""" & url & """超級鏈接:""" & _
              Target & """>" & url & "</a>"
    End If
  ElseIf UCase(urlType) = "EMAIL" Then
    strBuf = "<a href=""電子郵件地址:" & url & """鏈接目標:""" & _
            Target & """>" & url & "</a>"
  End If
  GetHref = strBuf
End Function

 

 

[1]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved