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?
Printable View
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?
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
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.
thak u so much for both.i will search thread for name value.
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.
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.Code:Option Explicit
Private Sub Form_Load()
Dim strURL As String, colParams As Collection
strURL = "http://www.test.com/file.php?id=1¶m1=Hello!¶m2=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
strParams = Split(Mid(URL, lonPos +1), "&")
To use the returned collection:
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: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
Original sample code becomesCode: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
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