[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

URL Encoding percentages (using percent signs)

I'm having trouble when I use the URLEncodedFormat tag with actual percentages. The decoded output is missing letters.

Any ideas?

Example text:
<cfset text = "20% split">

<cfquery...
INSERT...
VALUES('#URLEncodedFormat(text)#')
</cfquery
0
coldchillin
Asked:
coldchillin
  • 3
  • 3
  • 2
  • +2
1 Solution
 
SidFishesCommented:
are you sure you want to use urlencoded format? (your example code doesn't use any urls...just asking)

in any case your best bet would be to replace the % with the html equiv of &#37;

but note that you need to escape the single # so CF doesn't throw an error

<cfset foo = replacenocase(text, "%", "&##37;", "all")>


0
 
parikaaCommented:
hmmm.... is it an option to replace occurrences of %'s?

replace(text,"%","|","ALL") and after retrieval  add %'s  back in
0
 
parikaaCommented:
SidFishes answer is better...using the numeric entity.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
apollocvCommented:
I tested and I'm not seeing any missing letters.  What came out as your decoded output?
<cfset text="20% split">

<p>TEXT: #text#</p>

<p>ENCODED TEXT: #URLEncodedFormat(text)#</p>

<cfset encoded_text="#URLEncodedFormat(text)#">

<p>DECODED TEXT: #URLDecode(encoded_text)#</p>

Open in new window

0
 
apollocvCommented:
Also, you may need to set your character set because CF defaults to the character encoding of the URL scope.  For example,

<cfset encoded_text="#URLEncodedFormat(text,'UTF-8')#">

<p>DECODED TEXT: #URLDecode(encoded_text,'UTF-8')#</p>
0
 
coldchillinAuthor Commented:
Thanks for all of your responses.

Here's the full scope of what I"m doing. Using a Flex front-end, the user enters text into a text area,
and the back-end CF Remote Object than enters the data into a MSSQL table with an ntext data type.

The data is entered by using the URLEncodedFormat tag which encodes spaces, apostrophes, quotations, etc. etc. etc. I'm guessing because URLEncoding uses % signs, it doesn't handle encoding of percent signs well.

I was hoping to avoid manual replacement of % signs.

I was hoping, as Sid mentioned, that I could use the ## technique, such as %% to override and show the % but it looks like that's a no.

I wanted to avoid having to do a relace before inserting/updating, and before reading, because this is common, and used in various locations, on both flex and CF/CFML front-ends...
0
 
apollocvCommented:
Found this on the web:

An NTEXT character is encoded in UTF8 Unicode. A conversion from NTEXT to TEXT can result in data loss when the NTEXT value cannot be represented in the database character set.

Try setting the character set to UTF-8 when you are encoding it.
0
 
SidFishesCommented:
just to be clear

"that I could use the ## technique, such as %% to override and show the % but it looks like that's a no."

that's not what I was saying

you can't escape % with %% you must however escape any # that is not part of cfml ie: as above or color="#FFFFFF" because cf see # as an unclosed argument since # have special meaning in cf.

what I was saying is you need to use replacenocase to replace % with it's html equiv and since the html equiv has a single # in it you need to escape that.

try the UTF as mentioned by apollocv

but i would consider


<cfquery...
INSERT...
VALUES('#replacenocase(text, "%", "&##37;", "all")#')
</cfquery

(and of course you are using cfqueryparam right??)










0
 
gdemariaCommented:
> The data is entered by using the URLEncodedFormat tag

I have to ask, Why?

This does not seem like a best practice.  I would suggest always saving dating it it's true format and then formatting the output as needed when delivering it.

0
 
SidFishesCommented:
gd - that was my q in the first post as well :)
0
 
gdemariaCommented:
Right Sid, good question!


@coldchillin,  

 Inquiring minds want to know... why do you want to URLencode data in the database?

 Please explain so we can talk you out of it





0
 
coldchillinAuthor Commented:
I've always just used URLEncodedFormat because I'm under the basic assumption that it handles most text that would otherwise break the system when outputting the saved data.

And no, I'm not using cfqueryparam for these. This code is executed from a remote call within a flash application.

I'm trying to avoid having to replace all special characters (apostrophes, pound signs, percentages, ampersands, etc. etc.)

What's the best, most efficient method? I have an open field, sometimes a text box, or text area, or WYSIWYG editor, saving to an nvarchar or ntext, and I need it redisplayed.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now