Results 1 to 3 of 3

Thread: [RESOLVED] generate links from text

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Posts
    272

    Resolved [RESOLVED] generate links from text

    i was wondering the best way to generate a link from plain text, in other words 'link-a-fy' text.

    for example, the text says
    Code:
    http://vbforum.com
    but i want that converted to :

    Code:
    <a href="http://vbforum.com">http://vbforum.com</a>
    the number of links in the text file (or string) will vary.

    so let's say i have this text file:

    Code:
    The little red fox can be found at http://redfox.com and the little red hen can be found at http://littleredhen.com
    
    you can also get the red puppy at:
    
    http://redpuppies.net
    
    if you have any more questions send them to:
    
    [email protected]
    would read like this when revised:


    Code:
    The little red fox can be found at <a href="http://redfox.com">http://redfox.com</a> and the little red hen can be found at <a href="http://littleredhen.com">http://littleredhen.com</a>
    
    
    you can also get the red puppy at:
    
    <a href="http://redpuppies.net">http://redpuppies.net</a>
    
    if you have any more questions send them to:
    
    <a href="mailto:[email protected]">[email protected]</a>

    i know this sort of trick is as old as the hills, but it is new to me. hopefully it isnt too hard to do
    Last edited by inosent; Mar 26th, 2007 at 02:23 AM.

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: generate links from text

    Code:
    'API Guide
    Private Const S_FALSE = &H1
    Private Const S_OK = &H0
    'Only implemented as unicode...
    Private Declare Function IsValidURL Lib "URLMON.DLL" (ByVal pbc As Long, ByVal szURL As String, ByVal dwReserved As Long) As Long
    Public Function IsGoodURL(ByVal sURL As String) As Boolean
        'KPD-Team 2001
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        
        'The IsValidURL always expects a UNICODE string, but whenever
        'VB calls an API function, it converts the strings to ANSI strings.
        'That's why we're going to use a trick here. Before calling the function,
        'We're going to convert the unicode string to unicode so we get a double
        'unicode string.
        'Before VB calls the API function, it converts our double unicode string
        'to a normal unicode string; exactely what IsValidURL is expecting.
        sURL = StrConv(sURL, vbUnicode)
        'Now call the function
        IsGoodURL = (IsValidURL(ByVal 0&, sURL, 0) = S_OK)
    End Function
    
    'My code
    Private Sub Command1_Click()
    Dim Prts() As String
    Dim Lns() As String
    
        'split the text into lines
        Lns() = Split(Text1.Text, vbNewLine)
        'Go through each line
        For l = 0 To UBound(Lns)
            'Split each line into words
            Prts() = Split(Lns(l), " ")
            'go through each word
            For p = 0 To UBound(Prts)
                'if a word is a url (urls don't have spaces ;)), then we fix it up
                If IsGoodURL(Prts(p)) Then
                    'Fix it
                    Text1.Text = Replace(Text1.Text, Prts(p), "<a href=" & Chr(34) & Prts(p) & Chr(34) & ">" & Prts(p) & "</a>")
                End If
            Next
        Next
    End Sub
    A bit of code from API Guide as well

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Posts
    272

    Re: generate links from text

    perfect. very much appreciated

Posting Permissions

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



Click Here to Expand Forum to Full Width