How to encode strings in ColdFusion excluding dot

I'm 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 --->

      

Another urlEncode utf-8 encodes the characters "dot", "-" and "_". How can I prevent this scenario?

+3


source to share


4 answers


One answer can be found in this thread which

Use the [s] ColdFusion ReplaceList () function to "fix" the errors made, using URLEncodedFormat () to generate an RFC 3986 encoded URL string.



Code:

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

      

+2


source


This will solve it for you: -



<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>#replace(enc1, "%2E", ".", "ALL")#</cfoutput><br>
<cfoutput>#replace(enc2, "%2E", ".", "ALL")#</cfoutput><br>
<cfoutput>#replace(enc3, "%2E", ".", "ALL")#</cfoutput><br>

      

+3


source


You can use a period as a separator for lists and encode each item in the list separately. Something like that:

<cfset enc1="">
<cfloop list="#str#" index="i" delimiter=".">
  <cfset listAppend(enc1,urlEncodedFormat(i,"utf-8"),".")>
</cfloop>

      

+3


source


(Too long for comments ...)

I came across this thread while trying to implement RFC 3986 encoding . If you use the new encodeForURL function (instead of urlEncodedFormat ) it brings you closer, but requires a few tweaks.

According to RFC 3986:

  • Unreserved characters that should not be escaped: ALPHA / DIGIT / "-" / "." / "_" / "~" /

  • Spaces should be encoded like %20

    instead+

  • Reserved characters that must be escaped include: : / ? ## [ ] @ ! $ & ' ( ) * + , ; =

To make the results of EncodeForUR compatible:

  • Force asterisk encoding "*" (reserved character)
  • Reverse encoding of the tilde "~" (must not be encoded).
  • Change space encoding from "+" to "% 20":

Code:

encodedText = encodeForURL("space period.asterisk*");
encodedText = replaceList( encodedText , "%7E,+,*", "~,%20,%2A");

      

0


source







All Articles