从存储为带有CFML的字符串的url解析特定变量 [英] Parse a specific variable from a url stored as a string with CFML

查看:165
本文介绍了从存储为带有CFML的字符串的url解析特定变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从存储为字符串的网址解析特定的网址变量键值。看来你可以使用底层的java库coldfusion.util.HTMLTools在ACF下,但我需要它在Railo下工作。有没有另一种方式,或者是使用正则表达式最好的答案?

I want to parse a specific url variable key value from a url stored as a string. It seems that you can use the underlying java library coldfusion.util.HTMLTools under ACF, but I need it to work under Railo as well. Is there another way, or is using a regular expression the best answer?

我正在尝试检索url变量键的值,而不使用如下例所示的url格式的锚点。
http://example.com?key=134324625625435#gid=0

I'm trying to retrieve the value of the url variable key without the anchor in a url formatted like the following example. http://example.com?key=134324625625435#gid=0

推荐答案

我在发表评论Scott的回答,但是太长了,所以...

I was posting as a comment on Scott's answer, but it was getting too long, so...

John写道:


运行以下示例我的值为0,
但我认为应该是整个键值?

Running the following example I end up with the value of 0, but I think it should be the entire key value?



<cfoutput>
    <cfset theUrl = "https://docs.google.com/spreadsheet/ccc?key=0AthiZNZ73LBndUzRTUkplbmNhYWc##gid=0" />
    <cfset theUrl = listRest(theUrl, "?")>
    <cfloop list="#theUrl#" index="URLPiece" delimiters="&">
        Key: #listFirst(urlPiece, "=")# Value: #listLast(urlPiece, "=")# <br />
    </cfloop>
</cfoutput>

该示例URL失败的原因是它包含一个页面段

The reason for failure of that example URL is it contains a page segment (the bit after the hash), which needs to be stripped off before the query string can be parsed.

通过在<$中包含键/值部分来获取正确的变量/值也很重要c $ c> UrlDecode 。

此外,在值中有一个等号是完全可以接受的,因此 ?key == 应返回 = 作为值,这意味着将 ListLast 更改为a ListRest 并将 includeEmptyFields 设置为true。

Plus, it is perfectly acceptable to have an equal sign in the value, so ?key== should return = as the value, which means changing the ListLast to a ListRest and setting includeEmptyFields to true.

,如果你有一个查询字符串?a& b ,那么约定是将值设置为 true 空字符串 - 当前代码设置为键名称,这是错误的。

Also, if you have a querystring such as ?a&b then the convention is to set the value to either true or empty string - the current code is setting to the key name, which is wrong.

总之,这里有一个函数:

In summary, here's a function:

<cffunction name="getParamsFromUrlString" returntype="Struct" output=false >
    <cfargument name="UrlString" type="String" required />
    <cfargument name="Separator" type="String" default="?" />
    <cfargument name="Delimiter" type="String" default="&" />
    <cfargument name="AssignOp"  type="String" default="=" />
    <cfargument name="EmptyVars" type="String" default="" />

    <cfset var QueryString = ListRest( ListFirst( Arguments.UrlString , '##' ) , Arguments.Separator ) />
    <cfset var Result = {} />

    <cfloop index="local.QueryPiece" list=#QueryString# delimiters="#Arguments.Delimiter#">

        <cfif NOT find(Arguments.AssignOp,QueryPiece)>
            <cfset Result[ UrlDecode( QueryPiece ) ] = Arguments.EmptyVars />
        <cfelse>
            <cfset Result[ UrlDecode( ListFirst(QueryPiece,Arguments.AssignOp) ) ]
                =  UrlDecode( ListRest(QueryPiece,Arguments.AssignOp,true) ) />
        </cfif>
    </cfloop>

    <cfreturn Result />
</cffunction>

它可以简单地用作:

    <cfset theUrl = "https://docs.google.com/spreadsheet/ccc?key=0AthiZNZ73LBndUzRTUkplbmNhYWc##gid=0" />
    <cfset Data = getParamsFromUrlString( theUrl ) />
    <cfdump var=#Data# />

或者可以用于复杂的非标准网址字符串,例如:

Or it can be used on complicated non-standard URL strings like this:

    <cfset theUrl = "https://somewhere/index.jsp;x:145;y:54;z:1;f;d:%23%23;w:%3B" />
    <cfset Data = getParamsFromUrlString( theUrl , ';' , ';' , ':' , 'true' ) />
    <cfdump var=#Data# />

(希望)之间的一切。

这篇关于从存储为带有CFML的字符串的url解析特定变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆