Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    eek
    eek is offline
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Sofia, Bulgaria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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.

    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
    Last edited by eek; 07-03-2004 at 09:44 AM. Reason: changed topic title

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is one using ASP.Net
    ..But i am not sure it is what you're looking for... try it

    Code:
    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

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 [URL[http://www.example.com]]http://www.example.com[/URL]
    You could then Replace the Files as below!
    This Code is Untested

    strMessage = Request.Form("yourformfieldname")
    strMessage = Replace(strMessage, "[URL[", "<a href=""", 1, -1, 1)
    strMessage = Replace(strMessage, "]]", """>", 1, -1, 1)
    strMessage = Replace(strMessage, "[/URL]", "</a>", 1, -1, 1)

    Hope that Helps!

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    but that would have to come from a form.

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

  • #5
    eek
    eek is offline
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Sofia, Bulgaria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/webtec...110900-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 *opening msdn.microsoft.com/*

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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,

  • #7
    eek
    eek is offline
    New to the CF scene
    Join Date
    Jun 2003
    Location
    Sofia, Bulgaria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 so i'll write again when finish this thing

    thank you guys

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Why not use 4guys solution? It's not too long and is easy to use.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •