ColdFusion - 字符串到变量

问题描述 投票:5回答:4

我有一个这样的字符串,我从数据库中获取:

[email protected]&name=John

我想知道是否有一种简单的方法来提取数据并将它们分为两个变量,即用户和名称。

coldfusion cfml
4个回答
6
投票

@Marc,根据@Dan Bracuk的建议,您可以通过首先使用提到的分隔符作为&并再次使用=来分割字符串。请参考我的以下代码,它将对您有所帮助。我希望。

Runnable Example

<cfset yourInput= '[email protected]&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/

谢谢。


3
投票

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

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

<cfscript>
yourInput= '[email protected]&name=John';

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

writedump(result);
</cfscript>

2
投票

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

从本质上讲,您只是解析分隔列表两次并提取所需的部分。 ColdFusion允许使用多种方法。

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

string="[email protected]&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) ) ;

这将返回:

code1

然后,您只需从返回的结构中引用所需的变量即可。

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

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字符串,#=是两个肯定会导致你没有编码和正确处理问题。


1
投票

您可以使用“ListToArray”使用“&”作为分隔符来分割每个值,然后再次使用(或者如果只有2个值则使用ListFirst和ListLast)但这次使用“=”作为分隔符,这样您将[ “[email protected]”,“name = John”]为第一个结果,[[[user],[me @example.com]],[[name],[John]]]为第二个结果。

我通常建议使用结构而不是简单的变量,这里举一个例子

<cfscript>
    /* My Raw string */
    MyString = "[email protected]&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: [email protected] 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>

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

© www.soinside.com 2019 - 2024. All rights reserved.