home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / url_linker.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  3.9 KB  |  107 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. '***** BEGIN FUNCTIONS *****
  15. ' This function takes a string as input and links any http's it
  16. ' finds so that they are then clickable in a browser.  If only
  17. ' looks for http:// so www.asp101.com alone wouldn't link, but
  18. ' http://www.asp101.com would.
  19. Function LinkURLs(strInput)
  20.     Dim iCurrentLocation  ' Our current position in the input string
  21.     Dim iLinkStart        ' Beginning position of the current link
  22.     Dim iLinkEnd          ' Ending position of the current link
  23.     Dim strLinkText       ' Text we're converting to a link
  24.     Dim strOutput         ' Return string with links in it
  25.  
  26.     ' Start at the first character in the string
  27.     iCurrentLocation = 1
  28.  
  29.     ' Look for http:// in the text from the current position to
  30.     ' the end of the string.  If we find it then we start the
  31.     ' linking process otherwise we're done because there are no
  32.     ' more http://'s in the string.
  33.     Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
  34.         ' Set the position of the beginning of the link
  35.         iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)
  36.         
  37.         ' Set the position of the end of the link.  I use the
  38.         ' first space as the determining factor.
  39.         iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
  40.         
  41.         ' If we didn't find a space then we link to the
  42.         ' end of the string
  43.         If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
  44.  
  45.         ' Take care of any punctuation we picked up
  46.         Select Case Mid(strInput, iLinkEnd - 1, 1)
  47.             Case ".", "!", "?"
  48.                 iLinkEnd = iLinkEnd - 1
  49.         End Select
  50.         
  51.         ' This adds to the output string all the non linked stuff
  52.         ' up to the link we're curently processing.
  53.         strOutput = strOutput & Mid(strInput, iCurrentLocation, iLinkStart - iCurrentLocation)
  54.  
  55.         ' Get the text we're linking and store it in a variable
  56.         strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
  57.         
  58.         ' Build our link and append it to the output string
  59.         strOutput = strOutput & "<A HREF=""" & strLinkText & """>" & strLinkText & "</A>"
  60.  
  61.         ' Some good old debugging
  62.         'Response.Write iLinkStart & "," & iLinkEnd & "<BR>" & vbCrLf
  63.         
  64.         ' Reset our current location to the end of that link
  65.         iCurrentLocation = iLinkEnd
  66.     Loop
  67.     
  68.     ' Tack on the end of the string.  I need to do this so we
  69.     ' don't miss any trailing non-linked text
  70.     strOutput = strOutput & Mid(strInput, iCurrentLocation)
  71.     
  72.     ' Set the return value
  73.     LinkURLs = strOutput
  74. End Function 'LinkURLs
  75. '***** END FUNCTIONS *****
  76.  
  77.  
  78. Dim strUnlinked ' The variable to hold out text to be linked up.
  79.  
  80. ' Get the input string from wherever...
  81. ' It probably makes the most sense when this is read in from a
  82. ' DB or text file.  For illustration I'm setting it to this as
  83. ' a little plug for our partners!
  84. strUnlinked = "http://www.asp101.com is the best ASP site! <br />" & vbCrLf
  85. strUnlinked = strUnlinked & "You can get good XML content from http://www.xml101.com. <br />" & vbCrLf
  86. strUnlinked = strUnlinked & "Microsoft http://www.microsoft.com/ always has lots of good info too. <br />" & vbCrLf
  87.  
  88.  
  89. ' Show title for modified string
  90. Response.Write "<B>Original Text:</B><BR>" & vbCrLf
  91.  
  92. ' Show the original string for comparison:
  93. Response.Write strUnlinked
  94.  
  95.  
  96. ' Spacing!
  97. Response.Write vbCrLf & "<BR>" & vbCrLf & vbCrLf
  98.  
  99.  
  100. ' Show title for modified string
  101. Response.Write "<B>Text After Linking:</B><BR>" & vbCrLf
  102.  
  103. ' Call our function and write out the results:
  104. Response.Write LinkURLs(strUnlinked)
  105. ' That really is all there is to it folks!
  106. %>
  107.