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
I was posting as a comment on Scott’s answer, but it was getting too long, so…
John wrote:
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 theListLastto aListRestand settingincludeEmptyFieldsto true.Also, if you have a querystring such as
?a&bthen the convention is to set the value to eithertrueor empty string – the current code is setting to the key name, which is wrong.In summary, here’s a function:
It can be used as simply as:
Or it can be used on complicated non-standard URL strings like this:
And (hopefully) everything in between.