1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Detect url in a text and convert it to a clickable link

Discussion in 'C#' started by Kyriakos, Feb 4, 2011.

  1. #1
    Hi,

    Is there any ASP script that detects a text url in a string and convert it to a clickable link? Like all forums does it.

    example:
    http://www.google.com convert it to <a href="http://www.google.com">http://www.google.com</a>
    SEMrush
    thanks in advance
     
    Kyriakos, Feb 4, 2011 IP
    SEMrush
  2. gotlivechat

    gotlivechat Member

    Messages:
    516
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    35
    #2
    Not that I know of but through the use of INSTR function you could probably cobble something together quickly.
    Like
    IF INSTR(somestring,"http://")>0 THEN
    do something
    END IF
     
    gotlivechat, Feb 4, 2011 IP
  3. longcall911

    longcall911 Peon

    Messages:
    1,672
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Never heard of one. The main reason is probably that it would be extremely difficult to determine where the link text ends based on the fact that users often include spaces where they should not.

    In a forum, links are surrounded by opening and closing URL tags. If you are starting with that, then you can parse for each and add the anchor html to the beginning and end to form the link.
     
    longcall911, Feb 5, 2011 IP
  4. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
  5. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    This is a PHP scrip that converts url paths to clickable links
    $mystring = preg_replace("/(http[s]*:\/\/[\S]+)/", "<a href='\${1}' target='_blank' style='color:#3b5998;'><u>\${1}</u></a>", $string);
    PHP:
    Can anybody convert this to ASP?
     
    Kyriakos, Feb 6, 2011 IP
  6. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #6
    let me read the link for you then.....
    Dim MyString as String = Regex.Replace(OriginalString, "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])", Function(match As Match) String.Format("<a href=""{0}"">{0}</a>", match.ToString()))
    Code (markup):
     
    AstarothSolutions, Feb 7, 2011 IP
  7. virender.ets

    virender.ets Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have never heard about any script for this.
     
    virender.ets, Feb 7, 2011 IP
  8. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    The facebook chat does that. Any forum does that. I have the script in PHP but i want the same think in ASP.

    i have an error message with your script

     
    Kyriakos, Feb 7, 2011 IP
  9. breezewood15

    breezewood15 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Worked for me, thanks!
     
    breezewood15, Feb 7, 2011 IP
  10. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #10
    The code is in VB.Net not VBScript. I am sure a quick google will give you a Classic ASP version but it has been too many years since i last looked at VBScript to remember it.
     
    AstarothSolutions, Feb 7, 2011 IP
  11. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Not really convinced that one line of code counts as a "script"!
     
    AstarothSolutions, Feb 7, 2011 IP
  12. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #12
    ΑSP is very stupid and very old language. PHP is a 1000 times better.
     
    Kyriakos, Feb 7, 2011 IP
  13. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #13
    And .Net 10,000 times better :)
     
    AstarothSolutions, Feb 7, 2011 IP
  14. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #14
    The greatests web sites are builded in PHP. Anyway. It isn't any script in ASP for converting url paths to links?
     
    Kyriakos, Feb 7, 2011 IP
  15. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #15
    OK, i´ll use google for you again.

    How about:
    Function parseURL(strText)
      Set regEx = New RegExp
      regEx.Global = true
      regEx.IgnoreCase = True
      regEx.Pattern = "(\b(ht|f)tp(s?)://[^ ]+\b)"
      parseURL = regEx.Replace(strText,"<a href=""$1"">$1</a>")
    End Function
    Code (markup):
     
    AstarothSolutions, Feb 7, 2011 IP
  16. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #16
    error:

    This script is ASP script? I think that the "$" is for PHP variables.
     
    Kyriakos, Feb 7, 2011 IP
  17. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #17
    vb.net example:
    
            Dim str As String = "Test http://www.google.com abc def http://www.yahoo.com fjksk http://www.google.com <a href='http://www.google.com'>Google</a>"
            Dim res As String = ""
            Dim p1 As Integer = 0
            Dim p2 As Integer = 1
            Dim url As String = ""
            
            Do
                p1 = InStr(p2, str, " http://")
                If p1 > 0 Then
                    p2 = InStr(p1 + 2, str, " ")
                    If p2 = 0 Then p2 = str.Length
                    url = Mid(str, p1, (p2 - p1))
                    str = Replace(str, url, " <a href=""" & Trim(url) & """>" & Trim(url) & "</a>")
                Else
                    Exit Do
                End If
            Loop
            Response.Write(str)
    
    Code (markup):
    classic asp example:
    
            Dim str = "Test http://www.google.com abc def http://www.yahoo.com fjksk http://www.google.com <a href='http://www.google.com'>Google</a>"
            Dim res = ""
            Dim p1 = 0
            Dim p2 = 1
            Dim url = ""
            
            Do
                p1 = InStr(p2, str, " http://")
                If p1 > 0 Then
                    p2 = InStr(p1 + 2, str, " ")
                    If p2 = 0 Then p2 = str.Length
                    url = Mid(str, p1, (p2 - p1))
                    str = Replace(str, url, " <a href=""" & Trim(url) & """>" & Trim(url) & "</a>")
                Else
    [B]                Exit Loop[/B]
                End If
            Loop
            Response.Write(str)
    
    Code (markup):
    Most classic asp code will work in asp.net apart from a few minor differences. They main one here being asp.net is "Exit Do" and asp is "Exit Loop". Of course you could just use Do While p1 > 0, but didn't think of that earlier.
     
    camjohnson95, Feb 14, 2011 IP
  18. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,681
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Why use all theose lines of code -v- just one line?
    Dim MyString as String = Regex.Replace(OriginalString, "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])", Function(match As Match) String.Format("<a href=""{0}"">{0}</a>", match.ToString()))
    Code (markup):
     
    AstarothSolutions, Feb 14, 2011 IP
  19. -=hollyuser86=-

    -=hollyuser86=- Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    yes why not ??

    first of all add this line to the top

    using System.Text.RegularExpressions;

    than one function

    private string ConvetToLink(string text)
    {
    MatchEvaluator me = new MatchEvaluator(ToLink);
    Regex rx = new Regex(@"\b(ht|f)tp(s?)://\S+");
    return rx.Replace(text, me);
    }
    
    private string ToLink(Match m)
    {
    return string.Format("<a href=\"{0}\">{0}</a>", m.Value);
    }
    Code (markup):

    ConvetToLink function returns the output as you want...

    hope this will help you something !!!..

    And one more thing ASP.Net is not a language, its one type of environment for creating website.... actually every ASP.net has one language as its back-end like c#, VB.NET so, just tell in which language you want the code... (( if you are not familiar with the ASP.NET than see the first line of any .aspx page and at the top you will see something like <%@ Page Language="C#"... so, in this case my language is c#... check it ))

    By the way, my above code is in C# and if you want it in VB than use online code converter or other way that you prefer...
     
    Last edited: Feb 14, 2011
    -=hollyuser86=-, Feb 14, 2011 IP
  20. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #20
    I didn't say asp.net was a language. I'm not real good with Regex so I just use string functions. It's simply an alternative solution. And the original question was for an ASP Script that provides a solution for the problem. Also the error messages posted clearly indicate that the solution is required in "VBScript". And so far the only working solutions that I have seen are .Net.
     
    Last edited: Feb 14, 2011
    camjohnson95, Feb 14, 2011 IP