Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Here's code to get any part of a URL.

$
0
0
Just copy the code from the below code box and paste it into a module, and you can use it to get any part of a URL.
The part numbers are as follows.
0 = protocol
1 = username
2 = password
3 = host
4 = port
5 = path
6 = query
Any other part number results in an empty string being returned from the function.

If the requested part number is 4 (the port), but the URL doesn't contain a port number, then the returned value will be based on the protocol, as follows.
ftp = 21
gopher = 70
http = 80
https = 443
Any other protocol results in the returned port number being 0.

Code:

Public Function GetURLPart(ByVal URL As String, ByVal PartNumber As Long) As String
    Dim n As Long
    Dim n2 As Long
    Dim Protocol As String
    Dim PrePath As String
    Dim PreHost As String
    Dim UsernameAndPassword() As String
    Dim HostAndPort As String
    Dim HostAndPortSplit() As String
    Dim PathAndQuery As String
   
    n = InStr(1, URL, "://")
    If PartNumber = 0 Then
        GetURLPart = LCase$(Left$(URL, n - 1))
        Exit Function
    ElseIf PartNumber = 4 Then
        Protocol = LCase$(Left$(URL, n - 1))
    End If
    URL = Right$(URL, Len(URL) - n - 2)
   
    n = InStr(1, URL, "/")
    If n = 0 Then n = Len(URL) + 1
    PrePath = Left$(URL, n - 1)
   
    n2 = InStr(1, PrePath, "@")
    If n2 = 0 Then
        If PartNumber < 5 Then HostAndPort = PrePath
    Else
        PreHost = Left$(PrePath, n2 - 1)
        UsernameAndPassword() = Split(PreHost, ":")
        If PartNumber = 1 Then
            GetURLPart = UsernameAndPassword(0)
            Exit Function
        ElseIf PartNumber = 2 Then
            If UBound(UsernameAndPassword) > 0 Then GetURLPart = UsernameAndPassword(1)
            Exit Function
        End If
        If PartNumber < 5 Then HostAndPort = Right$(PrePath, Len(PrePath) - n2)
           
    End If
    If PartNumber < 5 Then
        HostAndPortSplit() = Split(HostAndPort, ":")
        If PartNumber = 3 Then
            GetURLPart = HostAndPortSplit(0)
        ElseIf PartNumber = 4 Then
            If UBound(HostAndPortSplit) > 0 Then
                GetURLPart = HostAndPortSplit(1)
            Else
                Select Case Protocol
                    Case "ftp"
                        GetURLPart = "21"
                    Case "gopher"
                        GetURLPart = "70"
                    Case "http"
                        GetURLPart = "80"
                    Case "https"
                        GetURLPart = "443"
                    Case Else
                        GetURLPart = "0"
                End Select
            End If
        End If
        Exit Function
    End If
   
    PathAndQuery = Right$(URL, Len(URL) - n + 1)
    n = InStr(1, PathAndQuery, "?")
    If PartNumber = 5 Then
        If n = 0 Then
            GetURLPart = PathAndQuery
        Else
            GetURLPart = Left$(PathAndQuery, n - 1)
        End If
    ElseIf PartNumber = 6 Then
        If n > 0 Then GetURLPart = Right$(PathAndQuery, Len(PathAndQuery) - n)
    End If
End Function


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>