从使用 CFML 存储为字符串的 url 中解析特定变量 [英] Parse a specific variable from a url stored as a string with CFML
问题描述
我想从存储为字符串的 url 中解析特定的 url 变量键值.看来您可以在 ACF 下使用底层的 java 库 Coldfusion.util.HTMLTools,但我也需要它在 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 变量键的值,格式如下例所示.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...
约翰写道:
运行以下示例,我最终得到的值为 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.
通过将键/值部分包装在 UrlDecode
中来获取正确的变量/值也很重要.
It's also important to get the correct variables/values by wrapping the key/value parts in UrlDecode
.
另外,值中有一个等号是完全可以接受的,所以 ?key==
应该返回 =
作为值,这意味着改变 ListLast
到 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# />
或者它可以用于复杂的非标准 URL 字符串,如下所示:
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# />
以及(希望)介于两者之间的一切.
And (hopefully) everything in between.
这篇关于从使用 CFML 存储为字符串的 url 中解析特定变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!