Results 1 to 4 of 4

Thread: [VB6] URL/Path String Manipulation Functions

Threaded View

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow [VB6] URL/Path String Manipulation Functions

    Some functions for manipulating strings containing URLs and local file addresses.

    Get parent address
    Returns the parent address from a URL. e.g.
    Code:
    ?GetParentAddress("http://www.domain.com/parentdirectory/subdirectory/")
    http://www.domain.com/parentdirectory
    VB Code:
    1. Public Function GetParentAddress(ByVal Address As String) As String
    2.     Dim lngCharCount    As Long
    3.     Dim lngBCount       As Long
    4.     Dim strOutput       As String
    5.  
    6.     strOutput$ = Replace(Address, "\", "/")
    7.     lngCharCount = Len(strOutput)
    8.  
    9.     If (Right$(strOutput, 1) = "/") Then strOutput = Left$(strOutput, lngCharCount - 1)
    10.  
    11.     lngCharCount = Len(strOutput)
    12.  
    13.     strOutput = Left$(strOutput, lngCharCount - lngBCount)
    14.     strOutput = Left$(strOutput, InStrRev(strOutput, "/", , vbTextCompare) - 1)
    15.  
    16.     GetParentAddress = strOutput
    17. End Function

    Get domain name
    Returns the domain name from a URL. e.g.
    Code:
    ?GetDomainName("http://www.domain.com/directory/page.html")
    www.domain.com
    VB Code:
    1. Public Function GetDomainName(ByVal Address As String) As String
    2.     Dim strOutput       As String
    3.     Dim strTemp         As String
    4.     Dim lngLoopCount    As Long
    5.     Dim lngBCount       As Long
    6.     Dim lngCharCount    As Long
    7.  
    8.     strOutput$ = Replace(Address, "\", "/")
    9.     lngCharCount = Len(strOutput)
    10.  
    11.     If (InStrB(1, strOutput, "/")) Then
    12.         Do Until ((strTemp = "/") Or (lngLoopCount = lngCharCount))
    13.             lngLoopCount = lngLoopCount + 1
    14.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    15.             lngBCount = lngBCount + 1
    16.         Loop
    17.     End If
    18.  
    19.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    20.     lngBCount = 0
    21.     strTemp = "/"
    22.  
    23.     If (InStrB(1, strOutput, "/")) Then
    24.         Do Until strTemp <> "/"
    25.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    26.             If strTemp = "/" Then lngBCount = lngBCount + 1
    27.         Loop
    28.     End If
    29.  
    30.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    31.     strOutput = Left$(strOutput, InStr(1, strOutput, "/", vbTextCompare) - 1)
    32.  
    33.     GetDomainName = strOutput
    34. End Function

    Canonicalize a URL
    Returns the canonicalized (friendly) version of the URL. e.g.
    Code:
    ?URLCanonicalize("http://www.domain.com/directory/page.html/")
    www.domain.com/directory/page.html
    VB Code:
    1. Public Function URLCanonicalize(ByVal pstrAddress As String) As String
    2.     Dim strOutput       As String
    3.     Dim strTemp         As String
    4.     Dim lngLoopCount    As Long
    5.     Dim lngBCount       As Long
    6.     Dim lngCharCount    As Long
    7.  
    8.     strOutput$ = Replace(pstrAddress, "\", "/")
    9.     lngCharCount = Len(strOutput)
    10.  
    11.     If (InStrB(1, strOutput, "/")) Then
    12.         Do Until ((strTemp = "/") Or (lngLoopCount = lngCharCount))
    13.             lngLoopCount = lngLoopCount + 1
    14.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    15.             lngBCount = lngBCount + 1
    16.         Loop
    17.     End If
    18.  
    19.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    20.     lngBCount = 0
    21.     strTemp = "/"
    22.  
    23.     If (InStrB(1, strOutput, "/")) Then
    24.         Do Until (strTemp <> "/")
    25.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    26.             If strTemp = "/" Then lngBCount = lngBCount + 1
    27.         Loop
    28.     End If
    29.  
    30.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    31.     lngBCount = 0
    32.     strTemp = "/"
    33.     lngCharCount = Len(strOutput)
    34.         If (InStrB(1, strOutput, "/")) Then
    35.         Do Until (strTemp <> "/")
    36.             strTemp = Mid$(strOutput, lngCharCount - lngBCount, 1)
    37.             If strTemp = "/" Then lngBCount = lngBCount + 1
    38.         Loop
    39.     End If
    40.  
    41.     strOutput = Left$(strOutput, Len(strOutput) - lngBCount)
    42.     strOutput = Replace(strOutput, "%20", " ")
    43.  
    44.     URLCanonicalize = strOutput
    45. End Function

    Remove illegal characters
    Returns the same address minus any characters not allowed in a Windows filename. e.g.
    Code:
    ?RemoveIllegals("Some *file* name/thing!")
    Some file namething!
    VB Code:
    1. Public Function RemoveIllegals(ByVal pstrCheckString As String) As String
    2.     Dim i               As Long
    3.     Dim strOutput       As String
    4.     Dim astrIllegals(8) As String
    5.  
    6.     strOutput = pstrCheckString
    7.  
    8.     astrIllegals(0) = "*"
    9.     astrIllegals(1) = "?"
    10.     astrIllegals(2) = "/"
    11.     astrIllegals(3) = "\"
    12.     astrIllegals(4) = ":"
    13.     astrIllegals(5) = "|"
    14.     astrIllegals(6) = "<"
    15.     astrIllegals(7) = ">"
    16.     astrIllegals(8) = Chr$(34)
    17.  
    18.     For i = 0 To 8
    19.         strOutput = Replace(strOutput, astrIllegals(i), vbNullString)
    20.     Next i
    21.  
    22.     RemoveIllegals = strOutput
    23. End Function

    Remove root name or get lowest level name
    Returns either the address minus path root, or the lowest-level identifier. e.g.
    Code:
    ?RemoveRootName("http://www.domain.com/parentdirectory/subdirectory/", False)
    parentdirectory/subdirectory
    
    ?RemoveRootName("http://www.domain.com/parentdirectory/subdirectory/", True)
    subdirectory
    VB Code:
    1. Public Function RemoveRootName(ByVal pstrPath As String, _
    2.                                ByVal pblnGetLowestLevelName As Boolean) _
    3.                               As String
    4.  
    5.     Dim strOutput       As String
    6.     Dim lngLoopCount    As Long
    7.     Dim lngBCount       As Long
    8.     Dim lngCharCount    As Long
    9.     Dim strTemp         As String
    10.  
    11.     strOutput = Replace(pstrPath, "\", "/")
    12.     lngCharCount = Len(strOutput)
    13.  
    14.     If (InStrB(1, strOutput, "/")) Then
    15.         Do Until ((strTemp = "/") Or (lngLoopCount = lngCharCount))
    16.             lngLoopCount = lngLoopCount + 1
    17.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    18.             lngBCount = lngBCount + 1
    19.         Loop
    20.     End If
    21.  
    22.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    23.     lngBCount = 0
    24.     strTemp = "/"
    25.     If (InStrB(1, strOutput, "/")) Then
    26.         Do Until (strTemp <> "/")
    27.             strTemp = Mid$(strOutput, lngBCount + 1, 1)
    28.             If strTemp = "/" Then lngBCount = lngBCount + 1
    29.         Loop
    30.     End If
    31.  
    32.     strOutput = Right$(strOutput, Len(strOutput) - lngBCount)
    33.     lngBCount = 0
    34.     strTemp = "/"
    35.     lngCharCount = Len(strOutput)
    36.  
    37.     If (InStrB(1, strOutput, "/")) Then
    38.         Do Until (strTemp <> "/")
    39.             strTemp = Mid$(strOutput, lngCharCount - lngBCount, 1)
    40.             If strTemp = "/" Then lngBCount = lngBCount + 1
    41.         Loop
    42.     End If
    43.  
    44.     strOutput = Left$(strOutput, Len(strOutput) - lngBCount)
    45.     strOutput = Right$(strOutput, Len(strOutput) - InStr(1, strOutput, "/", vbTextCompare))
    46.  
    47.     If (pblnGetLowestLevelName) Then _
    48.         strOutput = Right$(strOutput, Len(strOutput) - InStrRev(strOutput, "/"))
    49.  
    50.     strOutput = Replace(strOutput, "%20", " ")
    51.  
    52.     RemoveRootName = strOutput
    53. End Function

    Resolve environment variables
    Returns the path with any environment variables %environ% resolved. e.g.
    Code:
    ?ResolveEnvirons("%systemroot%\Fonts")
    C:\WINDOWS\Fonts
    VB Code:
    1. Public Function ResolveEnvirons(ByVal pstrText As String) As String
    2.     Dim i                   As Long
    3.     Dim bOpenMarker         As Boolean
    4.     Dim lngLeftMarkerPos    As Long
    5.     Dim strTemp             As String
    6.     Dim strResolveString    As String
    7.     Dim strTextLeft         As String
    8.     Dim strTextRight        As String
    9.  
    10.     For i = 0 To Len(pstrText)
    11.         strTemp = Right$(Left$(pstrText, i), 1)
    12.  
    13.         If (strTemp = "%") Then
    14.             If Not (bOpenMarker) Then
    15.                 bOpenMarker = True
    16.                 lngLeftMarkerPos = i
    17.  
    18.               Else
    19.                 bOpenMarker = False
    20.                 strResolveString = Left$(Right$(pstrText, Len(pstrText) - lngLeftMarkerPos), i - lngLeftMarkerPos - 1)
    21.  
    22.                 strTextLeft = Left$(pstrText, lngLeftMarkerPos - 1)
    23.                 strTextRight = Right$(pstrText, Len(pstrText) - i)
    24.  
    25.                 strResolveString = Environ(strResolveString)
    26.  
    27.                 pstrText = strTextLeft & strResolveString & strTextRight
    28.                 i = -1
    29.             End If
    30.         End If
    31.     Next i
    32.  
    33.     ResolveEnvirons = pstrText
    34. End Function
    Last edited by penagate; Jun 6th, 2005 at 03:30 AM.

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