SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    textbox to recognize url create link

    I'm working on making a form that when submitted checks to see if the user has typed in a link, if so it converts it to a hyperlink. This is for a news submission form on one of my sites. I don't really care if it is a popup like SPF has, or not. Just wanted to see if anyone here has done something similiar, and which route they took. Thanks!

  2. #2
    Phil fillup07's Avatar
    Join Date
    May 2002
    Location
    Jacksonville, FL
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes...
    Search this forum, as functions have been posted here pretty recently.

  3. #3
    SitePoint Zealot TheTank's Avatar
    Join Date
    May 2003
    Location
    Houston, TX
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a function that I use. I'm pretty sure I took it from a post here. Probably in the Funky Functions thread, but I haven't checked lately.

    Code:
    Function LinkURLs(tempTxt)
      Dim regEx
      Set regEx = New RegExp
      regEx.Global = True
      regEx.IgnoreCase = True
    
      'Hyperlink Email Addresses
      regEx.Pattern = "([_.a-z0-9-]+@[_.a-z0-9-]+\.[a-z]{2,3})" 
      tempTxt = regEx.Replace(tempTxt, "<a href=""mailto:$1"">$1</a>")
    
      'Hyperlink URL's
      regEx.Pattern = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
      tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" target = '_blank'>$1</a>")          
    
      'Make <a href="www = <a href="http://www
      tempTxt = Replace(tempTxt, "href=""www", "href=""http://www")
    
      LinkURLs = tempTxt
    End Function
    I think sometimes I dream in code.

  4. #4
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks Tank I'll give it a shot!

    I did search with no results. Must have used the wrong keywords

  5. #5
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You just wanted to hit 700!!!

  6. #6
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Dallas
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a whole PrepareText function that I use, and I used the function above in it, and it renders the text odd... before I used this function, the text rendered normally, like this:

    http://www.fark.com
    www.fark.com
    fark.com
    fakeaddress@yahoo.com

    After this function, the text is rendered like this:

    http://www.fark.com www.fark.com fark.com
    fakeaddress@yahoo.com

    ===========================
    I have never been good with regular expressions, is there anything in the above that would prevent the end line/line break from being converted properly to a <br> tag (that happens elsewhere in the function).

  7. #7
    Phil fillup07's Avatar
    Join Date
    May 2002
    Location
    Jacksonville, FL
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I was imagining things. Lol, I can assure you that it wasn't for 700 posts though... but thanks for pointing that out.

    Let me know how that function goes for you.

  8. #8
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It worked perfect man. Exactly what I needed. Thanks!

  9. #9
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That function does not work well with apostrophes ('). It also didn't like the "target='_blank'". I fixed both the errors:


    Code:
      
    Function LinkURLs(tempTxt)
       Dim regEx
       Set regEx = New RegExp
       regEx.Global = True
       regEx.IgnoreCase = True
     temptxt = replace(temptxt, "'", "''")
       'Hyperlink Email Addresses
       regEx.Pattern = "([_.a-z0-9-]+@[_.a-z0-9-]+\.[a-z]{2,3})"
       tempTxt = regEx.Replace(tempTxt, "<a href=""mailto:$1"">$1</a>")
       'Hyperlink URL's
       regEx.Pattern = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
       tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" target=""_blank"">$1</a>")
       'Make <a href="www = <a href="http://www
       tempTxt = Replace(tempTxt, "href=""www", "href=""http://www")
       LinkURLs = tempTxt
    End Function

  10. #10
    SitePoint Zealot TheTank's Avatar
    Join Date
    May 2003
    Location
    Houston, TX
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had not run across the apostrophy problem, but I have different functin created based if I want to target _blank or not. That's just my standard one.

    Good job on the apostrophes. I'll have to add that in.
    I think sometimes I dream in code.

  11. #11
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ahem, a mod, if you please.

    Code:
    Function LinkURLs(tempTxt, tgt)
       'Prefix underscore(_) to tgt
       tgt = "_" & tgt
       'If tgt is blank, set it to self.
       If Trim(tgt)="_" Then
       tgt = "_self"
       End If
    
    
       Dim regEx
       Set regEx = New RegExp
       regEx.Global = True
       regEx.IgnoreCase = True
     temptxt = replace(temptxt, "'", "''")
       'Hyperlink Email Addresses
       regEx.Pattern = "([_.a-z0-9-]+@[_.a-z0-9-]+\.[a-z]{2,3})"
       tempTxt = regEx.Replace(tempTxt, "<a href=""mailto:$1"">$1</a>")
       'Hyperlink URL's
       regEx.Pattern = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
    
       tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" target=" & tgt & ">$1</a>")
       'Make <a href="www = <a href="http://www
       tempTxt = Replace(tempTxt, "href=""www", "href=""http://www")
       LinkURLs = tempTxt
    End Function
    This one has an additional parameter(optional) & you can set target as you please, ie., self, top, blank, parent, etc. If none is specified, it is set as self. Also, don't use underscores(_) for specifying the target in the parameter. Its added automatically later.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  12. #12
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    We should all just keeping adding to this function until it is the "perfect" function lol We could even market it out to large firms and sell it for thousands!!!

  13. #13
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by williamsba
    We should all just keeping adding to this function until it is the "perfect"
    function lol We could even market it out to large firms and sell it for
    thousands!!!
    Ha Ha Ha
    So in fit for that, here's another mod.

    Another thing that I thought of & that was left in the previous mod. What if
    we wanna target a frame rather than the window attribute. So here's another mod.

    Code:
    Function LinkURLs(tempTxt, tgt, frm)
    'Prefix underscore(_) to tgt if frm=0 or null
    If (CInt(frm)=0) OR (CInt(frm)="") Then
    tgt = "_" & tgt
    End If
    'If tgt is blank, set it to self.
    If (Trim(tgt)="_") OR (Trim(tgt)="") Then
    tgt = "_self"
    End If
     
    Dim regEx
    Set regEx = New RegExp
    regEx.Global = True
    regEx.IgnoreCase = True
    temptxt = replace(temptxt, "'", "''")
    'Hyperlink Email Addresses
    regEx.Pattern = "([_.a-z0-9-]+@[_.a-z0-9-]+\.[a-z]{2,3})"
    tempTxt = regEx.Replace(tempTxt, "<a href=""mailto:$1"">$1</a>")
    'Hyperlink URL's
    regEx.Pattern = "((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"
    tempTxt = regEx.Replace(tempTxt, "<a href=""$1"" target=" & tgt & ">$1</a>")
    'Make <a href="www = <a href="http://www
    tempTxt = Replace(tempTxt, "href=""www", "href=""http://www")
    LinkURLs = tempTxt
    End Function
    Ok NOW, so this function above now also supports targetting frames. Just
    another optional parameter added by name of frm. If this parameter is set to
    any number but 0, it will not add an underscore(_) to the parameter tgt
    which specifies the target name. So if its omitted, then also it will add
    underscore(_).
    So, if you wanna target a frame, just set frm to any number, better be 1 of
    ease of things.


    So there!! Any more mods are more than welcome.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


Bookmarks

Posting Permissions

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