Parse a specific variable from a URL saved as a string with CFML

I want to parse a specific url key key value from a URL saved as a string. It looks like you can use the java coldfusion.util.HTMLTools core library under ACF, but I need it to work with Railo as well. Is there any other way or uses regex is the best answer?

I am trying to get the key value of a url variable without binding in a URL formatted like in the following example. http://example.com?key=134324625625435#gid=0

+3
source share
3 answers

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

John wrote:

, 0, , ?

<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- , ( ), , .

/, / UrlDecode.

, , ?key== = , ListLast a ListRest includeEmptyFields true.

, querystring, ?a&b, true, - , .

:

<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-, :

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

() .

+8

, , URL-, "".

, :

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

mbrusche:

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

, , :

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

( , , - , .)

+2

All Articles