Parse a specific variable from a url stored as a string with CFML

╄→尐↘猪︶ㄣ 提交于 2020-01-20 06:59:11

问题


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?

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


回答1:


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

John wrote:

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>

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.

It's also important to get the correct variables/values by wrapping the key/value parts in UrlDecode.

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.

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>

It can be used as simply as:

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

And (hopefully) everything in between.




回答2:


Any variables that are passed as part of the query string are available in the URL scope - which is a 'built in' structure.

Here is some sample code that will out put those variables in your example:

<cfoutput>
    #url.id# <br />
    #url.key# <br />
    #url.output#
</cfoutput>

Adding to my answer to match new information and mbrusche's example:

<cfset urlString = listRest( urldata, "?" ) />
<cfoutput>
    <cfloop list="#urlString#" index="URLPiece" delimiter="&">
        Key: #listFirst( urlPiece, "=" )# Value: #listLast( urlPiece, "=" (# <br />
    </cfloop>
</cfoutput>



回答3:


Having just re-read the question, if you are only dealing with a single specific variable, you can easily do this with a simple regex:

<cfset Value = rematch( '[?&]key=[^&##]+' , TheUrl ) />
<cfset Value = UrlDecode( ListRest( Value[1] , '=' , true ) ) />

(The other answer is of course still useful if you need a more general purpose solution.)



来源:https://stackoverflow.com/questions/10220795/parse-a-specific-variable-from-a-url-stored-as-a-string-with-cfml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!