View Full Version : Turn URLs written in text into hyperlinks
so i have some text, in which there are urls - http://example.org/ for example :) i want each of the urls to apper as a <a href="http://example.org/">http://example.org/</a>, so when shown in a web page it could be clicked :)
i searched google and i found some things about Regex, but it gave me an error when trying to run it.
Message = "something http://example.org/ something"
Return Regex.Replace(Message, "(\bhttp://[^ ]+\b)", "<a href=""$0"">$0</a>")
can you please help me, i am new to asp :( i would really appreciate it, thank you :)
07-05-2004, 02:27 AM
This is one using ASP.Net
..But i am not sure it is what you're looking for... try it :thumbsup:
Public Class RegExApp
Public Shared Sub Main()
Dim r As RegExApp = New RegExApp()
Public Sub New()
' Detecting EMAIL addresses. All occurrences will be processed.
Dim emailString As String = "My email address is firstname.lastname@example.org. " _
& "Don't spam me."
Console.WriteLine(vbCrLf & vbCrLf)
' Detecting Web sites. All occurrences will be processed.
Dim siteString As String = "My Web site is www.website.com; Visit us."
Public Function ActivateEmailAddress(emailString As String) As String
Dim buf As String = emailString
Dim patternEmail As String = "[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+"
Dim re As RegEx = New Regex(patternEmail)
If re.IsMatch(buf) Then
buf = re.Replace(buf, AddressOf MailToMatchEvaluator)
Public Function ActivateWebSiteUrl(siteString As String) As String
Dim buf As String = siteString
Dim patternSite As String = "\w*[\://]*\w+\.\w+\.\w+[/\w+]*[.\w+]*"
Dim re As RegEx = New Regex(patternSite)
If re.IsMatch(buf) Then
buf = re.Replace(buf, AddressOf WebSiteMatchEvaluator)
Private Function MailToMatchEvaluator(ByVal m As Match) As String
Dim sb As StringBuilder = New StringBuilder("<a href='mailto:")
Private Function WebSiteMatchEvaluator(ByVal m As Match) As String
Dim ub As UriBuilder = New UriBuilder(m.Value)
Dim sb As StringBuilder = New StringBuilder("<a href='")
To save some time you could make yourself some codes to make links such as the one above. To do what you wanted to do would be really hard.
For Example ]http://www.example.com
You could then Replace the Files as below!
This Code is Untested
strMessage = Request.Form("yourformfieldname")
strMessage = Replace(strMessage, "]", """>", 1, -1, 1)
strMessage = Replace(strMessage, "", "</a>", 1, -1, 1)
Hope that Helps!
07-05-2004, 02:50 AM
but that would have to come from a form.
i think what he's talking about is the whole page itself?
oh, i'm sorry i didn't mentioned it before - the whole text comes from a <textarea> in a form and the asp script writes it to a text file and the text file is then displayed in an html page. i just have problems displaying links, starting with http:// as <a href="http://...">http://...</a>
well, i guess i can give you a link: http://prodb.net/nick/tests/ :)
i actually found some code at 4guysfromrolla, the link: http://www.4guysfromrolla.com/webtech/tips/t110900-1.shtml, but i just wanted to know if there's a shorter code that does the same. what i need just has to deal with links starting with http://
and... hmm, i think i don't know how to run asp .net scripts on my server :D *opening msdn.microsoft.com/* :)
07-05-2004, 06:53 AM
Hmm, that looks okay there eek, I don't know why you would want a shorter one if it works perfectly either way?
Give sxar's a go, and see how you go.. if not try the .net one...
In relation to asp.net, if you have access to your server, download ASP.Net on IIS [6.0]. if not, ask your systems administrator to enable it for you.
Hope this helps,
ok, so the code sxar suggested really works, but it's not exactly what i want, because there are no in the text ;) anyway, i may try to modify it, 'cause it's nice :)
i have not yet tried the asp.net code that reubenb gave, i'll first have to deal with my iis :D so i'll write again when finish this thing
thank you guys :)
07-05-2004, 08:51 AM
Why not use 4guys solution? It's not too long and is easy to use.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.