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:
Option Explicit
Public Type URIType
Proto As String
Doucment As String
Host As String
Port As Integer
Params As String
End Type
Private Function GetPortNum(Proto As String) As Integer
Select Case Proto
Case "http": GetPortNum = 80
Case "ftp": GetPortNum = 21
End Select
End Function
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)
'Fix S
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)
'Extract host.
URI.Host = Mid$(S, 3, Count - 3)
Wend
'Fix S
S = Mid$(S, Count)
End If
'Get parms
For X = 1 To Len(S)
If Mid$(S, X, 1) = "?" Then
'Extract parms.
URI.Params = Mid$(S, X + 1)
'Fix S
S = Mid$(S, 1, X - 1)
End If
Next X
'Extract Page.
If Left(S, 1) = "/" Then
'Get page.
URI.Doucment = Mid$(S, 2)
End If
End If
'Return type.
ParseURI = URI
Next Count
End Function
Example add this code to a command buton's click event.
vb Code:
Dim URI As URIType
URI = ParseURI("http://www.host.com/page.html?id=5&name=ben")
With URI
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)
End With