Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert a text URL to a Hyperlink?

String Manipulation (RegEx)

How do I convert a text URL to a Hyperlink?

by  logicalunatic  Posted    (Edited  )
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.
Code:
<%

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")
    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=""http://"; & url & """ Target=""" & _
               Target & """>" & url & "</a>"
    Else
      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...
Code:
<%
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...

x_Question
&
x_Answer



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...
Code:
<%
    If rs("Question") = "" or IsNull(rs("Question")) Then
        x_Question = rs("Question")
    Else
        x_Question = InsertHyperlinks(rs("Question"))
    End If
    If rs("Answer") = "" or IsNull(rs("Answer")) Then
        x_Answer = rs("Answer")
    Else
        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.
Code:
<%
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
%>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top