Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: ASP (Active Server Pages) FAQ

String Manipulation (RegEx)

How do I convert a text URL to a Hyperlink? by logicalunatic
Posted: 20 Jan 04

IMPORTANT***The following Functions are not of my creation.  I found then on the 4GuysFromRolla.com website.  Only re-printing them here for convenience and to help point out the errors***IMPORTANT

The following is a function that will convert any URL in a string to a Hyperlink.


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"  ' Match URLs and emails
  objRegExp.IgnoreCase = True                   ' Set case insensitivity.
  objRegExp.Global = True                       ' Set global applicability.
  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")
      strBuf = strBuf & GetHref(objMatch.Value, "WEB", "_BLANK")
    End If
    iStart = iEnd+objMatch.Length+1
  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=""http://"; & url & """ Target=""" & _
               Target & """>" & url & "</a>"
      strBuf = "<a href=""" & url & """ Target=""" & _
               Target & """>" & url & "</a>"
    End If
  ElseIf UCase(urlType) = "EMAIL" Then
    strBuf = "<a href=""mailto:" & url & """ Target=""" & _
             Target & """>" & url & "</a>"
  End If
  GetHref = strBuf

End Function

Just in case you don't know how to use a Function in ASP here is how you would apply this Function to any particluar string...

Dim myString

myString = "Go to www.google.com"

This is my string: <%= InsertHyperlink(myString) %>

This Function does have two flaws...

Say you're grabbing text from a database and throwing it in two variables...


If either of the variables just happens to be blank it will produce a Data Type MisMatch error.  A way to get around this is...

    If rs("Question") = "" or IsNull(rs("Question")) Then
        x_Question = rs("Question")
        x_Question = InsertHyperlinks(rs("Question"))
    End If
    If rs("Answer") = "" or IsNull(rs("Answer")) Then
        x_Answer = rs("Answer")
        x_Answer = InsertHyperlinks(rs("Answer"))
    End If

rs is the Record Set object the data is coming from...

The other problem with this Function is apparent if the string already CONTAINS an actual HTML hyperlink.  The best way to avoid this problem is to first STRIP all html from the text, then apply the InsertHyperlink Function to it. Below is a function to strip the HTML from it.

Function stripHTML(strHTML)
'Strips the HTML tags from strHTML

  Dim objRegExp, strOutput
  Set objRegExp = New Regexp

  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "<(.|\n)+?>"

  'Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")

  'Replace all < and > with < and >
  strOutput = Replace(strOutput, "<", "<")
  strOutput = Replace(strOutput, ">", ">")

  stripHTML = strOutput    'Return the value of strOutput

  Set objRegExp = Nothing
End Function

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close