ASPAlliance Home
Advertise with us
About Prasad KVNM
Refer this Site
                                               
Home Convert URLs into Hyperlinks KV's Kool ASP
Search
Samples/Articles
Convert URLs into Hyperlinks

Advertisement Management Systems

AdRotator Component

Extending AdRotator Component

Simple Alternative to AdRotator

A more powerful Ad Management System

Opinion Poll

Suggestions/Snippets
Passing Special Characters through URL

Date delimiter for Access and SQL Server

CDBL instead of CSNG

Handling Database Errors

Avoid Partial Updates to the Database

Book Review
C# and the .NET Platform

Favorite Links
ASPLists.com
ASPNG.com
Computer Dictionary
LearnASP.com

Microsoft .NET
Search @ Google
Wrox Press
Freecode.com

Site by Prasad KVNM,
New Jersey, USA
prasad@kunisetty.com
last updated on
22nd Oct, 2001



Convert URLs into Hyperlinks - Source Code

I have written the conversion logic as a function, so that I can call it in any of my ASP pages.

fnConvertIntoURL.asp is the ASP page that contains the conversion function.

Source of fnConvertIntoURL.asp page:

<%

' Function to convert all URL strings into Hyperlinks in a given string.

FUNCTION convertIntoURL(inputStr)

startPos = 1
outputStr = ""

WHILE startPos < LEN(inputStr)

    posURL = InStr(startPos, LCASE(inputStr), "http://")
    IF posURL < 1 THEN
        posURL = InStr(startPos, LCASE(inputStr), "www.")
    END IF

    IF posURL > 0 THEN

        IF posURL = 1 THEN

            outputStr = outputStr & _
                MID(inputStr, startPos, posURL - startPos)

        ELSE

            outputStr = outputStr & _
                MID(inputStr, startPos, posURL -1 - startPos + 1)
        END IF

        outputStr = outputStr & " <a href="""
        posURLEnd = InStr(posURL, inputStr, " ")

        IF posURLEnd < 1 THEN
            posURLEnd = LEN(inputStr) + 1
        END IF

        urlStr = MID(inputStr, posURL, posURLEnd - posURL) 

        IF LEFT(LCASE(urlStr), 7) <> "http://" THEN

            linkStr = "http://" & urlStr

        ELSE

            linkStr = urlStr

          END IF

        outputStr = outputStr & linkStr & """>"
        outputStr = outputStr & urlStr & "</a>"

    ELSE

        outputStr = outputStr & MID(inputStr, startPos)
        posURLEnd = LEN(inputStr)

    END IF

    startPos = posURLEnd

WEND

convertIntoURL = outputStr

END FUNCTION

%>

To call this function from any ASP page, add the following line on top of your ASP page:

<!--#include file="fnConvertIntoURL.asp"-->

where "fnConvertIntoURL.asp" is the ASP page that contains the above conversion function.

And to convert URL strings into Hyperlinks in any given string, just pass the string to "convertIntoURL" as below:

newString = convertIntoURL(myString)

Hope this code will be useful for you in many applications.

Click here to see this code working!



Refer this site ASPAlliance.com |  Contact Us |  Join |  Advertise |  Best Viewed with IE 4.0 or above