ColdFusion - 字符串到变量
ColdFusion - String to variable
我有一个从数据库中得到的字符串:
user=me@example.com&name=John
我想知道是否有一种简单的方法可以提取数据并将它们放入用户和姓名两个变量中。
@Marc,根据@Dan Bracuk 的建议,您可以首先使用提到的定界符将字符串拆分为 &
,然后再次使用 =
。请参考我下面的代码,这将帮助你。我希望。
<cfset yourInput= 'user=me@example.com&name=John'>
<!--- Get the first value. I mean "user" part --->
<cfset splitFirstPart = listfirst(yourInput,'&', true)>
<cfset splitLastPart = listlast(yourInput, '&', true)>
<!--- Get the second part value --->
<!--- Above values are split by using & --->
<cfset user = listlast(splitFirstPart, '=', true)>
<Cfset name = listlast(splitLastPart, '=', true)>
<!---
Now we can again split the list by using =.
Now you can see the result.
--->
<cfoutput>
User : #user# <br/>
Name : #name#
</cfoutput>
如果您需要任何其他 CFML 函数和说明,请参阅 https://cfdocs.org/
谢谢。
这是我对如何解决这个问题的看法。
我喜欢将结构作为最终结果。我还喜欢将每个函数作为隐式循环使用。
<cfscript>
yourInput= 'user=me@example.com&name=John';
variables.result = {};
ListEach(yourInput,
function(item) { variables.result[listfirst(item, "=")] = listLast(item, "="); },
"&");
writedump(result);
</cfscript>
您可以使用 "ListToArray" 使用“&”作为分隔符来拆分每个值,然后再次使用(或者如果只有 2 个值,则使用 ListFirst 和 ListLast),但这次使用“=”作为分隔符, 这样你就会得到 ["user=me@example.com", "name=John"] 作为第一个结果,并且 [[[user],[me@example.com]],[[name],[John]] ]作为第二个。
我通常建议使用结构而不是简单的变量,这里有一个例子
<cfscript>
/* My Raw string */
MyString = "user=me@example.com&name=John";
/* Breaking my single string in multiple values */
MyArrayOfValues = ListToArray(MyString, "&");
/* Creating my struct o hold my values */
MyStruct = StructNew();
/* Interating over my values */
for (Value in MyArrayOfValues){
// First Interaction will be: user=me@example.com and the second will be name=John and etc...
/* Get My attribute name */
MyAttributeName = ListFirst(Value, "=");
/* Get My attribute value */
MyAttributeValue = ListLast(Value, "=");
/* Evaluate the values of you borth variables and asign each other */
Evaluate("MyStruct.#LCase(MyAttributeName)# = '#MyAttributeValue#'");
}
/* Here you can see your value printed as struct formed by 2 atributes, name and user, both in lower case */
writeDump(MyStruct);
/* Here one example how to use this data */
writeOutput("
Hi my name is #MyStruct.name# and my user is #MyStruct.user#!
");
</cfscript>
这种方法更通用,因为您的数据库中可能会有更多列,甚至可以将它与来自另一个数据库的其他数据一起使用,始终遵循相同的结构...由 & 和属性分隔的值和价值 =
要为未来的读者添加这个答案,有几种方法可以使这个答案更加动态。
本质上,您只是将一个定界列表解析两次并提取出您需要的部分。 ColdFusion 允许通过多种方式来做到这一点。
为便于说明,我已将其添加到原始字符串中。
string="user=me@example.com&name=John&somethingelse=42&foo&base64Msg=QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==" ;
我首选的解析方法是对我们来说是一个 CF 函数,它 return 是我需要的所有部分的结构。
public Struct function parseURLParamString( required String inURLStr ) {
/// Initialize the return struct.
var retStruct = {} ;
// Use listEach() function to iterate over the list with delim "&"
arguments.inURLStr.listeach(
function(item){
// listFirst gets 1st list element. listRest() gets all but 1st element. Delim "="
retStruct[listFirst(item,"=")] = listRest(item,"=") ;
}
, "&"
) ;
return retStruct ;
}
writeDump( parseURLParamString(string) ) ;
这将 return:
然后您可以从 returned 结构中引用您需要的变量。
但是如果您需要创建实际变量而不是从结构中提取它们,您可以这样做:
arguments.inURLStr.listeach(
function(item){
variables[listFirst(item,'=')] = listRest(item,"=") ;
}
, "&"
) ;
... 然后将您的外部函数更改为 return Void
或什么都不更改,然后从中删除结构。您可以引用 user = #user#
等变量。这需要你提前知道变量,而当传递一个特定的结构时,你可以只遍历结构并输出 keys/values。从技术上讲,您还可以遍历 variables
作用域,但其中可能还有很多其他变量。
如果需要,您也可以使用 getToken()
,但它具有与 listLast()
相同的限制。如果您的 value
包含第二个分隔符文本(如填充的 Base64 字符串),那么这些字符将被视为分隔符并从您的值中删除。对于 base64Msg = QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==
,getToken()
/listLast()
将 return QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg
,其中 listRest()
将给您 QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==
。或者更糟的是,如果字符位于字符串的中间,它将被截断。 ListLast()
删除定界列表的第一项并 return 删除列表的其余部分,因此如果您的字符串包含定界符,它将 return 完整值。
最后,由于这似乎是来自 URL 的字符串,您可能希望在将字符串存储到数据库之前对其进行清理和编码。
如果您保存编码值,它可能会将您的定界符变成它们的编码值。上面的函数只支持单字符定界符,所以不能像上面那样使用(除非在发送到拆分函数之前解码)。 listToArray
允许多字符定界符。所以这可能是拆分它们的一种方法。
最后,有很多字符允许 URL 字符串,#
和 =
这两个肯定会导致您在没有编码和正确的情况下出现问题处理。
我有一个从数据库中得到的字符串:
user=me@example.com&name=John
我想知道是否有一种简单的方法可以提取数据并将它们放入用户和姓名两个变量中。
@Marc,根据@Dan Bracuk 的建议,您可以首先使用提到的定界符将字符串拆分为 &
,然后再次使用 =
。请参考我下面的代码,这将帮助你。我希望。
<cfset yourInput= 'user=me@example.com&name=John'>
<!--- Get the first value. I mean "user" part --->
<cfset splitFirstPart = listfirst(yourInput,'&', true)>
<cfset splitLastPart = listlast(yourInput, '&', true)>
<!--- Get the second part value --->
<!--- Above values are split by using & --->
<cfset user = listlast(splitFirstPart, '=', true)>
<Cfset name = listlast(splitLastPart, '=', true)>
<!---
Now we can again split the list by using =.
Now you can see the result.
--->
<cfoutput>
User : #user# <br/>
Name : #name#
</cfoutput>
如果您需要任何其他 CFML 函数和说明,请参阅 https://cfdocs.org/
谢谢。
这是我对如何解决这个问题的看法。
我喜欢将结构作为最终结果。我还喜欢将每个函数作为隐式循环使用。
<cfscript>
yourInput= 'user=me@example.com&name=John';
variables.result = {};
ListEach(yourInput,
function(item) { variables.result[listfirst(item, "=")] = listLast(item, "="); },
"&");
writedump(result);
</cfscript>
您可以使用 "ListToArray" 使用“&”作为分隔符来拆分每个值,然后再次使用(或者如果只有 2 个值,则使用 ListFirst 和 ListLast),但这次使用“=”作为分隔符, 这样你就会得到 ["user=me@example.com", "name=John"] 作为第一个结果,并且 [[[user],[me@example.com]],[[name],[John]] ]作为第二个。
我通常建议使用结构而不是简单的变量,这里有一个例子
<cfscript>
/* My Raw string */
MyString = "user=me@example.com&name=John";
/* Breaking my single string in multiple values */
MyArrayOfValues = ListToArray(MyString, "&");
/* Creating my struct o hold my values */
MyStruct = StructNew();
/* Interating over my values */
for (Value in MyArrayOfValues){
// First Interaction will be: user=me@example.com and the second will be name=John and etc...
/* Get My attribute name */
MyAttributeName = ListFirst(Value, "=");
/* Get My attribute value */
MyAttributeValue = ListLast(Value, "=");
/* Evaluate the values of you borth variables and asign each other */
Evaluate("MyStruct.#LCase(MyAttributeName)# = '#MyAttributeValue#'");
}
/* Here you can see your value printed as struct formed by 2 atributes, name and user, both in lower case */
writeDump(MyStruct);
/* Here one example how to use this data */
writeOutput("
Hi my name is #MyStruct.name# and my user is #MyStruct.user#!
");
</cfscript>
这种方法更通用,因为您的数据库中可能会有更多列,甚至可以将它与来自另一个数据库的其他数据一起使用,始终遵循相同的结构...由 & 和属性分隔的值和价值 =
要为未来的读者添加这个答案,有几种方法可以使这个答案更加动态。
本质上,您只是将一个定界列表解析两次并提取出您需要的部分。 ColdFusion 允许通过多种方式来做到这一点。
为便于说明,我已将其添加到原始字符串中。
string="user=me@example.com&name=John&somethingelse=42&foo&base64Msg=QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==" ;
我首选的解析方法是对我们来说是一个 CF 函数,它 return 是我需要的所有部分的结构。
public Struct function parseURLParamString( required String inURLStr ) {
/// Initialize the return struct.
var retStruct = {} ;
// Use listEach() function to iterate over the list with delim "&"
arguments.inURLStr.listeach(
function(item){
// listFirst gets 1st list element. listRest() gets all but 1st element. Delim "="
retStruct[listFirst(item,"=")] = listRest(item,"=") ;
}
, "&"
) ;
return retStruct ;
}
writeDump( parseURLParamString(string) ) ;
这将 return:
然后您可以从 returned 结构中引用您需要的变量。
但是如果您需要创建实际变量而不是从结构中提取它们,您可以这样做:
arguments.inURLStr.listeach(
function(item){
variables[listFirst(item,'=')] = listRest(item,"=") ;
}
, "&"
) ;
... 然后将您的外部函数更改为 return Void
或什么都不更改,然后从中删除结构。您可以引用 user = #user#
等变量。这需要你提前知道变量,而当传递一个特定的结构时,你可以只遍历结构并输出 keys/values。从技术上讲,您还可以遍历 variables
作用域,但其中可能还有很多其他变量。
如果需要,您也可以使用 getToken()
,但它具有与 listLast()
相同的限制。如果您的 value
包含第二个分隔符文本(如填充的 Base64 字符串),那么这些字符将被视为分隔符并从您的值中删除。对于 base64Msg = QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==
,getToken()
/listLast()
将 return QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg
,其中 listRest()
将给您 QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==
。或者更糟的是,如果字符位于字符串的中间,它将被截断。 ListLast()
删除定界列表的第一项并 return 删除列表的其余部分,因此如果您的字符串包含定界符,它将 return 完整值。
最后,由于这似乎是来自 URL 的字符串,您可能希望在将字符串存储到数据库之前对其进行清理和编码。
如果您保存编码值,它可能会将您的定界符变成它们的编码值。上面的函数只支持单字符定界符,所以不能像上面那样使用(除非在发送到拆分函数之前解码)。 listToArray
允许多字符定界符。所以这可能是拆分它们的一种方法。
最后,有很多字符允许 URL 字符串,#
和 =
这两个肯定会导致您在没有编码和正确的情况下出现问题处理。