I recently uncovered a sample of VBscript ASP that does the one thing I never learned about - advanced string handling.
This one I was able to understand enough to modify it to work with a system I am building for my website. The code now searches the text given it and turns any word with a "[" or a "]" around it [like this] into a hyperlink with the word as a value.
now as I don't fully understand the code I think I need to remove the leading [. Could someone talk me through what I need to do so as I can carry on learning and understanding this?
Obviously a value has to be gained from somewhere but I am currently concerned only with the function.
the current result is that both [ and ] stay and the [ is used to make the hyper link. I need to know how I should remove the [ and ] all-togeather.Code:<% '***** BEGIN FUNCTIONS ***** Function LinkWords(strInput) Dim iCurrentLocation ' The 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 being converted to a link Dim strOutput ' Return string with the links in it ' Start at the first character in the string iCurrentLocation = 1 ' Look for [ 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 ['s in the string. Do While InStr(iCurrentLocation, strInput, "[", 1) <> 0 ' Set the position of the beginning of the link iLinkStart = InStr(iCurrentLocation, strInput, "[", 1) ' Set the position of the end of the link. ie Find ] iLinkEnd = InStr(iLinkStart, strInput, "]", 1) ' If we didn't find a ] 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=""" & "reader.asp?art=" & 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 LinkWords = strOutput End Function 'LinkURLs '***** END FUNCTIONS ***** Dim strUnlinked ' The variable to hold out text to be linked up. ' Call our function and write out the results: Response.Write LinkWordss(strUnlinked) %>





Reply With Quote