Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Web Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Turn text into clickable links

18 Years Ago kudosvisions 0 Tallied Votes 196 Views Share

Hi Guys.
I am new to this forum so I thaught i'd post a useful code snippet. it changes text to clickable html links.

Hope it comes in handy.

Pete:)

asp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<%
'***** BEGIN FUNCTIONS *****
' This function takes a string as input and links any www's it
' finds so that they are then clickable in a browser.  

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, "www", 1) <> 0
		' Set the position of the beginning of the link
		iLinkStart = InStr(iCurrentLocation, strInput, "www", 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=""http://" & strLinkText & """ target=""_blank"">" & 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 'LinkURLs
'***** END FUNCTIONS *****


Dim strUnlinked ' The variable to hold out text to be linked up.

' Get the input string from wherever...
' It probably makes the most sense when this is read in from a
' DB or text file.!
strUnlinked = "http://www.kudosvisions.com is the best Web site! <br />" & vbCrLf
strUnlinked = strUnlinked & "Microsoft http://www.microsoft.com/ always has lots of good info too. <br />" & vbCrLf


' Show title for modified string
Response.Write "<B>Original Text:</B><BR>" & vbCrLf

' Show the original string for comparison:
Response.Write strUnlinked


' Spacing!
Response.Write vbCrLf & "<BR>" & vbCrLf & vbCrLf


' Show title for modified string
Response.Write "<B>Text After Linking:</B><BR>" & vbCrLf

' Call our function and write out the results:
Response.Write LinkURLs(strUnlinked)
' That really is all there is to it folks!
%>

</body>
</html>
About the Author
Member Avatar for kudosvisions
kudosvisions 0 Newbie Poster

A self confessed Web Production GEEK

Member Avatar for Dani
Dani 4,562 The Queen of DaniWeb Administrator Featured Poster Premium Member
18 Years Ago

Welcome to DaniWeb and thank you for your contribution! I went ahead and made a small edit by removing the tag you had surrounding the code snippet. Within this code snippet library, that is done automatically for you. It still needs to be done in the forums though. Thanks![code=asp] tag you had surrounding the code snippet. Within this code snippet library, that is done automatically for you. It still needs to be done in the forums though. Thanks!

Member Avatar for pecp
pecp 0 Newbie Poster
13 Years Ago

Cool just what I needed. Thanks.

Reply to this topic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar asp:loginview issue 1
  • Member Avatar ASP.NET Panel DefaultButton not working 5
  • Member Avatar mouse over ,sub menu appear 1
  • Member Avatar Asp.net C#,Show Message box after saving the data into db(sqlserver) 1
  • Member Avatar How to... 4
  • Member Avatar asp:BoundField as link? 2
  • Member Avatar is a variable set? 6
  • Member Avatar ASP vbscript recordset paging headache 6
  • Member Avatar Content management system 4
  • Member Avatar ASP.NET ItemCommand Event is not firing 2
  • Member Avatar script running without errors, but not doing tasks 3
  • Member Avatar ASP.NET Registration Page 11
  • Member Avatar trouble with script registering data 3
  • Member Avatar ASP.Net Security 101 Part 1 5
  • Member Avatar good blog engine buiding tutorial needed 2
  • Member Avatar asp.net Label is an ambiguous reference 4
  • Member Avatar Get HTTP Request Headers and Body 6
  • Member Avatar asp.net 3
  • Member Avatar ASP.NET with C# 6
  • Member Avatar asp code generated as text, needed to be seen as asp code again. databses backup 5
Not what you need?

Reach out to all the awesome people in our web development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC