...

View Full Version : Turn URLs written in text into hyperlinks



eek
07-03-2004, 10:39 AM
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.


Dim Message
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 :)

reubenb
07-05-2004, 03:27 AM
This is one using ASP.Net
..But i am not sure it is what you're looking for... try it :thumbsup:



Imports System
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic

Public Class RegExApp
Public Shared Sub Main()
Dim r As RegExApp = New RegExApp()
End Sub

Public Sub New()
' Detecting EMAIL addresses. All occurrences will be processed.
Dim emailString As String = "My email address is email@email.com. " _
& "Don't spam me."
Console.WriteLine(ActivateEmailAddress(emailString))

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."
Console.WriteLine(ActivateWebSiteUrl(siteString))
End Sub

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)
End If
Return buf
End Function

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)
End If
Return buf
End Function

Private Function MailToMatchEvaluator(ByVal m As Match) As String
Dim sb As StringBuilder = New StringBuilder("<a href='mailto:")
sb.Append(m.Value)
sb.Append("'>")
sb.Append(m.Value)
sb.Append("</a>")
Return sb.ToString()
End Function

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='")
sb.Append(ub.ToString())
sb.Append("'>")
sb.Append(m.Value)
sb.Append("</a>")
Return sb.ToString()
End Function
End Class

sxar
07-05-2004, 03:41 AM
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!

reubenb
07-05-2004, 03:50 AM
but that would have to come from a form.

i think what he's talking about is the whole page itself?

eek
07-05-2004, 07:45 AM
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/* :)

reubenb
07-05-2004, 07: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,

eek
07-05-2004, 08:10 AM
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 :)

glenngv
07-05-2004, 09:51 AM
Why not use 4guys solution? It's not too long and is easy to use.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum