Search code examples
encodingcoldfusioncharacter-encodingurlencodecoldfusion-9

How to encode strings in ColdFusion excluding the "dot"


I am looking for a way to encode strings with ColdFusion but excluding the ".". This is what I have tried so far:

<!--- Test area --->
<cfset str="ChrisTilghmanFirstFlash.Eflv">
<cfset str1="Chris Tilghman First Flash.Eflv">
<cfset str2="Chris-Tilghman First_Flash.Eflv">
<cfset enc1 = urlEncodedFormat(str,"utf-8" )>
<cfset enc2 = urlEncodedFormat(str1,"utf-8")>
<cfset enc3 = urlEncodedFormat(str2,"utf-8")>
<cfoutput>#enc1#</cfoutput><br>
<cfoutput>#enc2#</cfoutput><br>
<cfoutput>#enc3#</cfoutput><br>
<!--- END test area --->

The urlEncode utf-8 other encodes the "dot", "-" and "_" characters too. How do I prevent this scenario?


Solution

  • One answer can be found in this thread, which

    Use[s] ColdFusion's ReplaceList() function to "correct" the errors made by URLEncodedFormat() to produce an RFC 3986 compliant URL encoded string.

    Code:

    <cfset string = replacelist(urlencodedformat(string), "%2D,%2E,%5F,%7E", "-,.,_,~")>