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
LVL 1
coldchillinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.