用 query_string 拆分多个参数的最简单方法是什么?
Whats the simpelst way to split multiple paramters with query_string?
我正在用这样的 URL 呼叫代理:
http://myserver/db.nsf/testagent?openagent¶m1=ONE¶m2=TWO
代理'testagent'应该尽快获取参数值。我读到 'query_string' 应该这样做,但是代码变得很长,当我真的只想获得值 ONE 和 TWO.有没有更简单/更快的方法来获取这些值?
我的 Lotus 脚本代码如下所示:
Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim arg As String, p1 As Long
arg = s.DocumentContext.Query_String(0)
p1 = InStr(arg, "&")
If p1 = 0 Then
Print "ERROR: No arugment."
Exit Sub
Else
arg = LCase(Mid$(arg, p1 + 1))
' (more code): Split again the ARG to & (more code)
' (more code): Split the values again to =, after that fill the 2 element as param 1 (ONE) and param 2 (TWO)
End If
您的问题标题中已有答案:只需拆分字符串...
此外,我通常使用列表来处理此类任务,因为它们访问起来既方便又快捷。我会把它作为一个单独的函数取出来,以便能够重用代码并使所有内容更具可读性:
Function GetQuerystringParameters( strQueryString as String ) as Variant
Dim varParams as Variant
Dim lstrParams List as String
varParams = Split( strQueryString, "&")
Forall strParam in varParams
If Instr(strParam, "=") > 0 then
lstrParams( strtoken( strParam, "=", 1))= strtoken( strParam, "=", 2)
End If
End ForAll
GetQuerystringParameters = lstrParams
End Function
现在您可以简单地获取正确的参数,参数甚至不需要按正确的顺序排列:
Dim urlParams as Variant
Dim yourArg as String
urlParams = GetQuerystringParameters( arg )
If isElement( urlParams( "param1" ) ) then
yourArg = urlParams( "param1" )
Else
Print "Error: no param1"
Exit Sub
End If
希望对您有所帮助。
编辑:我什至可能会从中制作一个 class 以进一步减少代码并使其更好地可重用:
Class UrlParameters
Private lstrParams List as String
Sub New( strQueryString as String )
Call Me.GetParameters( strQueryString )
End Sub
Private Sub GetParameters( strQs as String )
Dim varParams as Variant
varParams = Split( strQs, "&")
Forall strParam in varParams
If Instr(strParam, "=") > 0 then
Me.lstrParams( strtoken( strParam, "=", 1)) = strtoken( strParam, "=", 2)
End If
End ForAll
End Sub
Function getUrlParameter( strParam as String ) as String
If Iselement(Me.lstrParams(strParam)) then
getUrlParameter = Me.lstrParams(strParam)
Else
GetUrlParameter = ""
End If
End Function
End Class
你可以这样使用 class:
Dim myParams as UrlParameters
Set myParams = New UrlParameters( arg )
param1 = myParams.getUrlParameter("param1")
param2 = myParams.getUrlParameter("param2")
为了快速简单,请尝试对查询字符串的 DECODED 版本使用基本字符串处理:
'Get the decoded query string value and extract p1 and p2
arg = s.DocumentContext.Query_String_Decoded(0)
p1 = StrRight(arg, "param1=")
If instr(p1, "&") Then p1 = StrLeft(p1, "&")
p2 = StrRight(arg, "param2=")
If instr(p2, "&") Then p2 = StrLeft(p1, "&")
因此 p1="一" 和 p2="二"。解码的 CGI 变量应该为您处理任何字符编码。 (如果你不使用它,要么编写你自己的解码器,要么永远不要在你的参数中使用 non-alpanumeric 个字符)。
当然你可以把它抽象成一个函数,但是你会失去一点速度,除非你有很多参数,否则真的不需要函数:
Function GetParam(arg, px) as astring
GetParam = StrRight(arg, px & "=")
If instr(GetParam, "&") Then GetParam = StrLeft(GetParam, "&")
End Function
然后简单地调用它:
arg = s.DocumentContext.Query_String_Decoded(0)
p1 = GetParam(arg, "param1")
p2 = GetParam(arg, "param2")
将参数放在列表中并没有错,但是在解析出参数后很少需要重复使用参数,尽管列表速度很快,但它们会增加开销和复杂性。
我正在用这样的 URL 呼叫代理:
http://myserver/db.nsf/testagent?openagent¶m1=ONE¶m2=TWO
代理'testagent'应该尽快获取参数值。我读到 'query_string' 应该这样做,但是代码变得很长,当我真的只想获得值 ONE 和 TWO.有没有更简单/更快的方法来获取这些值?
我的 Lotus 脚本代码如下所示:
Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim arg As String, p1 As Long
arg = s.DocumentContext.Query_String(0)
p1 = InStr(arg, "&")
If p1 = 0 Then
Print "ERROR: No arugment."
Exit Sub
Else
arg = LCase(Mid$(arg, p1 + 1))
' (more code): Split again the ARG to & (more code)
' (more code): Split the values again to =, after that fill the 2 element as param 1 (ONE) and param 2 (TWO)
End If
您的问题标题中已有答案:只需拆分字符串...
此外,我通常使用列表来处理此类任务,因为它们访问起来既方便又快捷。我会把它作为一个单独的函数取出来,以便能够重用代码并使所有内容更具可读性:
Function GetQuerystringParameters( strQueryString as String ) as Variant
Dim varParams as Variant
Dim lstrParams List as String
varParams = Split( strQueryString, "&")
Forall strParam in varParams
If Instr(strParam, "=") > 0 then
lstrParams( strtoken( strParam, "=", 1))= strtoken( strParam, "=", 2)
End If
End ForAll
GetQuerystringParameters = lstrParams
End Function
现在您可以简单地获取正确的参数,参数甚至不需要按正确的顺序排列:
Dim urlParams as Variant
Dim yourArg as String
urlParams = GetQuerystringParameters( arg )
If isElement( urlParams( "param1" ) ) then
yourArg = urlParams( "param1" )
Else
Print "Error: no param1"
Exit Sub
End If
希望对您有所帮助。
编辑:我什至可能会从中制作一个 class 以进一步减少代码并使其更好地可重用:
Class UrlParameters
Private lstrParams List as String
Sub New( strQueryString as String )
Call Me.GetParameters( strQueryString )
End Sub
Private Sub GetParameters( strQs as String )
Dim varParams as Variant
varParams = Split( strQs, "&")
Forall strParam in varParams
If Instr(strParam, "=") > 0 then
Me.lstrParams( strtoken( strParam, "=", 1)) = strtoken( strParam, "=", 2)
End If
End ForAll
End Sub
Function getUrlParameter( strParam as String ) as String
If Iselement(Me.lstrParams(strParam)) then
getUrlParameter = Me.lstrParams(strParam)
Else
GetUrlParameter = ""
End If
End Function
End Class
你可以这样使用 class:
Dim myParams as UrlParameters
Set myParams = New UrlParameters( arg )
param1 = myParams.getUrlParameter("param1")
param2 = myParams.getUrlParameter("param2")
为了快速简单,请尝试对查询字符串的 DECODED 版本使用基本字符串处理:
'Get the decoded query string value and extract p1 and p2
arg = s.DocumentContext.Query_String_Decoded(0)
p1 = StrRight(arg, "param1=")
If instr(p1, "&") Then p1 = StrLeft(p1, "&")
p2 = StrRight(arg, "param2=")
If instr(p2, "&") Then p2 = StrLeft(p1, "&")
因此 p1="一" 和 p2="二"。解码的 CGI 变量应该为您处理任何字符编码。 (如果你不使用它,要么编写你自己的解码器,要么永远不要在你的参数中使用 non-alpanumeric 个字符)。
当然你可以把它抽象成一个函数,但是你会失去一点速度,除非你有很多参数,否则真的不需要函数:
Function GetParam(arg, px) as astring
GetParam = StrRight(arg, px & "=")
If instr(GetParam, "&") Then GetParam = StrLeft(GetParam, "&")
End Function
然后简单地调用它:
arg = s.DocumentContext.Query_String_Decoded(0)
p1 = GetParam(arg, "param1")
p2 = GetParam(arg, "param2")
将参数放在列表中并没有错,但是在解析出参数后很少需要重复使用参数,尽管列表速度很快,但它们会增加开销和复杂性。