url_linker.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack Then
myTextBox.Text = "http://www.asp101.com is the best ASP " _
& "site, but http://www.4guysfromrolla.com and " _
& "http://www.15seconds.com are awesome too."
End If
End Sub
Sub myButton_OnClick(Sender As Object, E As EventArgs)
myLiteral.Text = LinkURLs(myTextBox.Text)
End Sub
' This function takes a string as input and links any http's it
' finds so that they are then clickable in a browser. If only
' looks for http:// so www.asp101.com alone wouldn't link, but
' http://www.asp101.com would.
Function LinkURLs(strInput)
Dim iCurrentLocation ' Our current position in the input string
Dim iLinkStart ' Beginning position of the current link
Dim iLinkEnd ' Ending position of the current link
Dim strLinkText ' Text we're converting to a link
Dim strOutput ' Return string with links in it
' Start at the first character in the string
iCurrentLocation = 1
' Look for http:// in the text from the current position to
' the end of the string. If we find it then we start the
' linking process otherwise we're done because there are no
' more http://'s in the string.
Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
' Set the position of the beginning of the link
iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)
' Set the position of the end of the link. I use the
' first space as the determining factor.
iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
' If we didn't find a space then we link to the
' end of the string
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
' Take care of any punctuation we picked up
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case ".", "!", "?"
iLinkEnd = iLinkEnd - 1
End Select
' This adds to the output string all the non linked stuff
' up to the link we're curently processing.
strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)
' Get the text we're linking and store it in a variable
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
' Build our link and append it to the output string
strOutput = strOutput & "<a href=""" & strLinkText & """>" & strLinkText & "</a>"
' Some good old debugging
'Response.Write iLinkStart & "," & iLinkEnd & "<br />" & vbCrLf
' Reset our current location to the end of that link
iCurrentLocation = iLinkEnd
Loop
' Tack on the end of the string. I need to do this so we
' don't miss any trailing non-linked text
strOutput = strOutput & Mid(strInput, iCurrentLocation)
' Set the return value
LinkURLs = strOutput
End Function
</script>
<html>
<head>
<title>ASP.NET URL Linker Sample</title>
</head>
<body>
<form runat="server">
<p>Enter some text containing hyperlinks for the script to link up:</p>
<asp:TextBox id="myTextBox" runat="server"
TextMode = "MultiLine"
Columns = "60"
Rows = "5"
/>
<br />
<asp:Button id="myButton" runat="server"
Text = "Submit Text"
OnClick = "myButton_OnClick"
/>
<br />
<br />
<p><strong>Text After Linking:</strong></p>
<p>
<asp:Literal id="myLiteral" runat="server" />
</p>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/url_linker_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>