' RETOURNE TOUS LES PARAMS ET VALEURS DANS UN TABLEAU BI-DIMENSIONNEL
Function GetParamsAndValues(ByVal sUrl As String) As String()
' necessite la fonction 'GetValueParam'
If LenB(sUrl) Then
' on cherche le ?
Dim iPos As Integer
iPos = InStr(1, sUrl, "?")
If iPos Then
' trouvé, on ne split que les arguments
Dim asParam() As String
asParam = Split("&" & Right$(sUrl, Len(sUrl) - iPos), "=")
' on nettoie/formate
Dim i As Integer, iCount As Integer
iCount = 0
For i = 0 To UBound(asParam)
iPos = InStrRev(asParam(i), "&")
If iPos Then
asParam(i) = Right$(asParam(i), Len(asParam(i)) - iPos)
If Len(asParam(i)) Then iCount = iCount + 1
Else
asParam(i) = vbNullString
End If
Next i
' résultats? on crée le tableau de retour et enregistre les valeurs
If iCount Then
Dim asRes() As String
ReDim asRes(0 To iCount - 1, 0 To 1)
iCount = -1
For i = 0 To UBound(asParam)
If Len(asParam(i)) Then
iCount = iCount + 1
asRes(iCount, 0) = asParam(i)
asRes(iCount, 1) = GetValueParam(sUrl, asParam(i))
End If
Next i
' retour / nettoyage
GetParamsAndValues = asRes
Erase asRes
Erase asParam
End If
End If
End If
End Function
'
' RETOURNE LA VALEUR D'UN PARAM
Function GetValueParam(ByVal sUrl As String, ByVal sParam As String) As String
GetValueParam = vbNullString
' validité de la demande
If (Not Len(sUrl) = 0) And (Not Len(sParam) = 0) And (InStr(1, sParam, "&") = 0) And (InStr(1, sParam, "=") = 0) Then
' on cherche le ?
Dim iPos As Integer
iPos = InStr(1, sUrl, "?")
If iPos Then
' trouvé, on ne récupère que les arguments et on facile la recherche
sUrl = "&" & Right$(sUrl, Len(sUrl) - iPos) & "&"
iPos = InStr(1, sUrl, "&" & sParam & "=")
If iPos Then
' trouvé? on retourne de cette position jusqu'au prochain arg
iPos = iPos + Len(sParam) + 2
GetValueParam = Mid$(sUrl, iPos, InStr(iPos + 1, sUrl, "&") - iPos)
End If
End If
End If
End Function
'
' EXEMPLE D'UTILISATION
'
Private Sub Form_Load()
Const URL1 As String = "www.test.com/index.php?p1=v1&p2=v2&p3=trois&prm4&cinq=exemple&=quoi&=oui&dernier=enfin"
Const URL2 As String = "http://www.abcdef.com/abc.abc?abc=abcd://abc.abcdef.abc/abc/abc_/xxxx.abc&carte=yyyy"
' TOUS LES PARAMS / VALEURS DE LA PREMIèRE URL
Dim i As Integer, RET() As String
RET = GetParamsAndValues(URL1)
For i = 0 To UBound(RET)
Debug.Print "PARAM : '" & RET(i, 0) & "' VALEUR : '" & RET(i, 1) & "'"
Next i
' PARAMS NOMMéS ('ABC' et 'CARTE') DE LA 2e URL
Debug.Print GetValueParam(URL2, "abc")
Debug.Print GetValueParam(URL2, "carte")
End Sub