Results 1 to 6 of 6

Thread: want to parse query strin

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    want to parse query strin

    hi its simple in vb.net but i want solution in vb6.
    i have following URL
    http://www.abc.co.in/book.aspx?uid=53591736317909859

    what i want to do is.i want to extract "uid" value from above url.
    can anyone help me how to do it?

  2. #2
    Lively Member
    Join Date
    Feb 2008
    Location
    Denmark
    Posts
    113

    Re: want to parse query strin

    from what you wrote, i made thise

    Code:
    Private Function extractUID(urlTxt As String) As String
        Dim uidLoc As Integer
        Dim returntext As String
        
        uidLoc = InStr(urlTxt, "uid=")
        returntext = Right(urlTxt, Len(urlTxt) - (uidLoc + 4))
        extractUID = returntext
    End Function

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: want to parse query strin

    A more generic solution would be to get the query string first (string to the right of ?), then split on & to get the parameter-value pairs, and finally split each pair on = to get the parameter name and its corresponding value.

    Up to you if you will return just value (string) for given parameter value or if you will return a collection instead. Either way, you will also have to decode the HTTP encoding on the value, e.g. %20 converted to spaces. There's a thread here regarding encode/decode, do a search.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: want to parse query strin

    thak u so much for both.i will search thread for name value.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: want to parse query strin

    Here is something I wrote you can use following leinad's suggestion. However, I don't have the time to handle escape sequences from the URL (replacing %20 with a space, etc.). If you have a list of those I could add it in, though.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim strURL As String, colParams As Collection
        
        strURL = "http://www.test.com/file.php?id=1&param1=Hello!&param2=hi&uid=1234&hello=7755"
        
        Set colParams = S_GET(strURL)
        MsgBox colParams("uid"), , "uid"
        MsgBox colParams("param1"), , "param1"
        
        Set colParams = Nothing
    End Sub
    
    Private Function S_GET(ByRef URL As String) As Collection
        Dim colRet As Collection, lonPos As Long
        Dim strParams() As String, l As Long
        Dim strValue() As String
        
        Set colRet = New Collection
        lonPos = InStr(1, URL, "?")
        
        If lonPos > 0 Then
            strParams = Split(URL, "&")
            For l = 0 To UBound(strParams)
                If InStr(1, strParams(l), "=") > 0 Then
                    strValue = Split(strParams(l), "=")
                    colRet.Add strValue(1), strValue(0)
                End If
            Next l
        End If
        
        Set S_GET = colRet
        Set colRet = Nothing
        Erase strParams, strValue
    End Function
    Paste this into a new VB project and run it. Then you can just copy the S_GET function into your program and use it.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: want to parse query strin

    strParams = Split(Mid(URL, lonPos +1), "&")

    To use the returned collection:
    Code:
    Dim colSample As Collection
    
    Set colSample = S_GET("http://www.abc.co.in/book.aspx?uid=53591736317909859")
    If Not colSample Is Nothing Then
       MsgBox colSample("uid")
    End If
    Collection allows for processing more than one URL parameter. Since trying to extract a non-existent collection item will throw an error, you can also use a wrapper to extract from collection.
    Code:
    Private Function GetParamValue(ByRef Params As Collection, Key As Variant) As String
       If (Params Is Nothing) Then Exit Function 'vbNullString returned
    
    On Error GoTo ErrHandler
       GetParamValue = Params(Key) 'Variant allows for access based on item index instead of param name
                  'it would be better to access by parameter name though in case parameter order in URL changes  
    On Error GoTo 0
       Exit Function
    
    ErrHandler:
       Err.Clear
       'vbNullString returned
    End Function
    Original sample code becomes
    Code:
    Dim colSample As Collection
    
    Set colSample = S_GET("http://www.abc.co.in/book.aspx?uid=53591736317909859&sample=123")
    MsgBox GetParamValue(colSample, "uid")
    MsgBox GetParamValue(colSample, 1) '1st item is uid, order of parameters in URL may change
    MsgBox GetParamValue(colSample, "sample")
    MsgBox GetParamValue(colSample, "invalid")  'returns vbNullString
    End If
    Last edited by leinad31; Jun 19th, 2008 at 12:35 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