5

我有一个从数据库中得到的这样的字符串:

user=me@example.com&name=John

我想知道是否有一种简单的方法可以提取数据并将它们放入两个变量中,用户和名称。

4

4 回答 4

6

@Marc,根据@Dan Bracuk 的建议,您可以先使用提到的分隔符 as&再使用 as来拆分字符串=。请参考我的以下代码,这将对您有所帮助。我希望。

可运行示例

<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/

谢谢。

于 2019-04-23T13:27:29.860 回答
3

这是我对如何解决这个问题的看法。

我喜欢有一个结构作为最终结果。我也喜欢将每个函数用作隐式循环。

<cfscript>
yourInput= 'user=me@example.com&name=John';

variables.result = {};
ListEach(yourInput,
   function(item) { variables.result[listfirst(item, "=")] = listLast(item, "="); },
   "&");

writedump(result);
</cfscript>
于 2019-04-23T16:25:41.480 回答
2

为了给未来的读者添加这个答案,有几种方法可以使它更具活力。

从本质上讲,您只是两次解析一个分隔列表并取出您需要的部分。ColdFusion 提供了几种方法来做到这一点。

为了说明,我已添加到原始字符串中。

string="user=me@example.com&name=John&somethingelse=42&foo&base64Msg=QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==" ;

我首选的解析方法是给我们一个 CF 函数,它返回一个包含我需要的所有部分的结构。

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) ) ;

这将返回:

代码1

然后您可以从返回的结构中引用您需要的变量。

但是,如果您需要创建实际变量而不是从结构中提取它们,您可以这样做:

arguments.inURLStr.listeach( 
    function(item){ 
      variables[listFirst(item,'=')] = listRest(item,"=") ;
    }
    , "&"
) ;

...然后将您的外部函数更改为返回Void或不返回并从中删除结构。您可以引用变量,例如user = #user#. 这需要您提前知道变量,而在传递特定结构时,您可以循环遍历结构并输出键/值。从技术上讲,您也可以遍历variables范围,但其中也可能有很多其他变量。

如果您愿意,您也可以使用getToken(),但它具有相同的限制listLast()。如果您value包含第二个分隔符文本(如填充的 Base64 字符串),那么这些字符将被视为分隔符并被排除在您的值之外。因为base64Msg = QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==getToken()/listLast()会回来QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg,哪里listRest()会给你QmVFeGNlbGxlbnRUb0VhY2hPdGhlcg==。或者更糟糕的是,如果字符在字符串的中间,它会被截断。ListLast()删除分隔列表的第一项并返回列表的其余部分,因此如果您的字符串包含分隔符,它将返回完整值。

最后,由于这似乎是来自 URL 的字符串,因此您可能希望在将字符串存储到数据库之前对其进行清理和编码。

如果您保存编码值,它可能会将您的分隔符转换为它们的编码值。上面的函数只支持单字符分隔符,所以不能像上面那样使用(除非在发送到拆分函数之前解码)。listToArray允许多字符分隔符。所以这可能是拆分它们的一种方法。

最后,有很多字符可以作为 URL 字符串,如果没有编码#=正确处理,两个字符肯定会导致您出现问题。

于 2019-04-23T19:22:15.530 回答
1

您可以使用“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>

这种方式是更通用的方法,因为您的数据库中可能会有更多列,甚至可以将它与来自另一个数据库的其他数据一起使用,始终遵循相同的结构...由 & 分隔的值和由 = 分隔的属性和值

于 2019-04-23T16:57:01.553 回答