User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP section within the Web Development category of DaniWeb, a massive community of 332,773 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,844 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP advertiser: Lunarpages ASP Web Hosting
Dec 20th, 2006
Views: 6,973
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
Last edited : Feb 7th, 2007.
asp Syntax | 5 stars
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <title>Untitled Document</title>
  7. </head>
  8.  
  9. <body>
  10. <%
  11. '***** BEGIN FUNCTIONS *****
  12. ' This function takes a string as input and links any www's it
  13. ' finds so that they are then clickable in a browser.
  14.  
  15. Function LinkURLs(strInput)
  16. Dim iCurrentLocation ' Our current position in the input string
  17. Dim iLinkStart ' Beginning position of the current link
  18. Dim iLinkEnd ' Ending position of the current link
  19. Dim strLinkText ' Text we're converting to a link
  20. Dim strOutput ' Return string with links in it
  21.  
  22. ' Start at the first character in the string
  23. iCurrentLocation = 1
  24.  
  25. ' Look for http:// in the text from the current position to
  26. ' the end of the string. If we find it then we start the
  27. ' linking process otherwise we're done because there are no
  28. ' more http://'s in the string.
  29. Do While InStr(iCurrentLocation, strInput, "www", 1) <> 0
  30. ' Set the position of the beginning of the link
  31. iLinkStart = InStr(iCurrentLocation, strInput, "www", 1)
  32.  
  33. ' Set the position of the end of the link. I use the
  34. ' first space as the determining factor.
  35. iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
  36.  
  37. ' If we didn't find a space then we link to the
  38. ' end of the string
  39. If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
  40.  
  41. ' Take care of any punctuation we picked up
  42. Select Case Mid(strInput, iLinkEnd - 1, 1)
  43. Case ".", "!", "?"
  44. iLinkEnd = iLinkEnd - 1
  45. End Select
  46.  
  47. ' This adds to the output string all the non linked stuff
  48. ' up to the link we're curently processing.
  49. strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)
  50.  
  51. ' Get the text we're linking and store it in a variable
  52. strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
  53.  
  54. ' Build our link and append it to the output string
  55. strOutput = strOutput & "<A HREF=""http://" & strLinkText & """ target=""_blank"">" & strLinkText & "</A>"
  56.  
  57. ' Some good old debugging
  58. 'Response.Write iLinkStart & "," & iLinkEnd & "<BR>" & vbCrLf
  59.  
  60. ' Reset our current location to the end of that link
  61. iCurrentLocation = iLinkEnd
  62. Loop
  63.  
  64. ' Tack on the end of the string. I need to do this so we
  65. ' don't miss any trailing non-linked text
  66. strOutput = strOutput & Mid(strInput, iCurrentLocation)
  67.  
  68. ' Set the return value
  69. LinkURLs = strOutput
  70. End Function 'LinkURLs
  71. '***** END FUNCTIONS *****
  72.  
  73.  
  74. Dim strUnlinked ' The variable to hold out text to be linked up.
  75.  
  76. ' Get the input string from wherever...
  77. ' It probably makes the most sense when this is read in from a
  78. ' DB or text file.!
  79. strUnlinked = "http://www.kudosvisions.com is the best Web site! <br />" & vbCrLf
  80. strUnlinked = strUnlinked & "Microsoft http://www.microsoft.com/ always has lots of good info too. <br />" & vbCrLf
  81.  
  82.  
  83. ' Show title for modified string
  84. Response.Write "<B>Original Text:</B><BR>" & vbCrLf
  85.  
  86. ' Show the original string for comparison:
  87. Response.Write strUnlinked
  88.  
  89.  
  90. ' Spacing!
  91. Response.Write vbCrLf & "<BR>" & vbCrLf & vbCrLf
  92.  
  93.  
  94. ' Show title for modified string
  95. Response.Write "<B>Text After Linking:</B><BR>" & vbCrLf
  96.  
  97. ' Call our function and write out the results:
  98. Response.Write LinkURLs(strUnlinked)
  99. ' That really is all there is to it folks!
  100. %>
  101.  
  102. </body>
  103. </html>
Comments (Newest First)
cscgal | The Queen of DaniWeb | Feb 7th, 2007
Welcome to DaniWeb and thank you for your contribution! I went ahead and made a small edit by removing the [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!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 9:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC