Results 1 to 3 of 3

Thread: URIParser

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    URIParser

    Hi this is something I just came up with this small bit of code to parse an url into it's parts. Proto,Host,Document,Paort and Params
    Code is commented hope it comes in handy.

    To use create new project add a new module and add this code to the module.

    Comments suggestions welcome.

    vb Code:
    1. Option Explicit
    2.  
    3. Public Type URIType
    4.     Proto As String
    5.     Doucment As String
    6.     Host As String
    7.     Port As Integer
    8.     Params As String
    9. End Type
    10.  
    11. Private Function GetPortNum(Proto As String) As Integer
    12.     Select Case Proto
    13.         Case "http": GetPortNum = 80
    14.         Case "ftp": GetPortNum = 21
    15.     End Select
    16. End Function
    17.  
    18. Public Function ParseURI(Source As String) As URIType
    19. Dim S As String
    20. Dim Count As Integer
    21. Dim X As Integer
    22. Dim URI As URIType
    23.    
    24.     'String to scan
    25.     S = Source
    26.    
    27.     For Count = 1 To Len(S)
    28.         If Mid$(S, Count, 1) = ":" Then
    29.             'Extract protocol and port.
    30.             URI.Proto = Left$(S, Count - 1)
    31.             URI.Port = GetPortNum(URI.Proto)
    32.             'Fix S
    33.             S = Mid$(S, Count + 1)
    34.             'Check for //
    35.             If Left(S, 2) = "//" Then
    36.                 Count = 3
    37.                 While (Count <= Len(S) And Mid$(S, Count, 1) <> "/")
    38.                     Count = (Count + 1)
    39.                     'Extract host.
    40.                     URI.Host = Mid$(S, 3, Count - 3)
    41.                 Wend
    42.                 'Fix S
    43.                 S = Mid$(S, Count)
    44.             End If
    45.            
    46.             'Get parms
    47.             For X = 1 To Len(S)
    48.                 If Mid$(S, X, 1) = "?" Then
    49.                     'Extract parms.
    50.                     URI.Params = Mid$(S, X + 1)
    51.                     'Fix S
    52.                     S = Mid$(S, 1, X - 1)
    53.                 End If
    54.             Next X
    55.             'Extract Page.
    56.             If Left(S, 1) = "/" Then
    57.                 'Get page.
    58.                 URI.Doucment = Mid$(S, 2)
    59.             End If
    60.         End If
    61.         'Return type.
    62.         ParseURI = URI
    63.     Next Count
    64. End Function

    Example add this code to a command buton's click event.

    vb Code:
    1. Dim URI As URIType
    2.  
    3.     URI = ParseURI("http://www.host.com/page.html?id=5&name=ben")
    4.    
    5.     With URI
    6.         Call MsgBox(.Proto, vbOKOnly Or vbInformation)
    7.         Call MsgBox(.Host, vbOKOnly Or vbInformation)
    8.         Call MsgBox(.Doucment, vbOKOnly Or vbInformation)
    9.         Call MsgBox(.Params, vbOKOnly Or vbInformation)
    10.         Call MsgBox(.Port, vbOKOnly Or vbInformation)
    11.     End With
    Last edited by BenJones; Feb 5th, 2012 at 06:09 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: URIParser

    Making a lot of assumptions and missing some fine points there.

    See http://en.wikipedia.org/wiki/URI_scheme#Examples

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: URIParser

    Thanks for the link I have a read should give me some ideas.

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