Feb 5th, 2012, 04:57 PM
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.
Public Type URIType
Proto As String
Doucment As String
Host As String
Port As Integer
Params As String
Private Function GetPortNum(Proto As String) As Integer
Select Case Proto
Case "http": GetPortNum = 80
Case "ftp": GetPortNum = 21
Public Function ParseURI(Source As String) As URIType
Dim S As String
Dim Count As Integer
Dim X As Integer
Dim URI As URIType
'String to scan
S = Source
For Count = 1 To Len(S)
If Mid$(S, Count, 1) = ":" Then
'Extract protocol and port.
URI.Proto = Left$(S, Count - 1)
URI.Port = GetPortNum(URI.Proto)
S = Mid$(S, Count + 1)
'Check for //
If Left(S, 2) = "//" Then
Count = 3
While (Count <= Len(S) And Mid$(S, Count, 1) <> "/")
Count = (Count + 1)
URI.Host = Mid$(S, 3, Count - 3)
S = Mid$(S, Count)
For X = 1 To Len(S)
If Mid$(S, X, 1) = "?" Then
URI.Params = Mid$(S, X + 1)
S = Mid$(S, 1, X - 1)
If Left(S, 1) = "/" Then
URI.Doucment = Mid$(S, 2)
ParseURI = URI
Example add this code to a command buton's click event.
Dim URI As URIType
URI = ParseURI("http://www.host.com/page.html?id=5&name=ben")
Call MsgBox(.Proto, vbOKOnly Or vbInformation)
Call MsgBox(.Host, vbOKOnly Or vbInformation)
Call MsgBox(.Doucment, vbOKOnly Or vbInformation)
Call MsgBox(.Params, vbOKOnly Or vbInformation)
Call MsgBox(.Port, vbOKOnly Or vbInformation)
Last edited by BenJones; Feb 5th, 2012 at 05:09 PM.
Feb 6th, 2012, 08:41 AM
Making a lot of assumptions and missing some fine points there.
Feb 6th, 2012, 11:31 AM
Thanks for the link I have a read should give me some ideas.
Click Here to Expand Forum to Full Width
Survey posted by VBForums.