Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Decoding" Strings... Is there a better way? 2

Status
Not open for further replies.

Nevermoor

Programmer
Jul 25, 2003
218
US
Hi,

I am stuck in the unfortunate situation of having to pass a string to another page through the URL. This is unfortunate as characters like "&" will cut off the end of the string, but it is even worse because "'" breaks the application. The first solution that popped into mind last Friday was to convert the offending characters into non-offending strings. I have a JavaScript program that turns:

"Bob's Trucks & Hardware"
into
"Bob_SQUOTE_s Trucks _AMPERSAND_ Hardware" to put in the URL.

I'm trying to write a function on the ASP page to "decode" this, but it is returning
"Bob's Trucks &Hardware" (notice the missing space).

This is important because I have to match the string against a database to see if it is contained or not, so it is a fairly significant problem. I am certainly open to other approaches to the problem, and any help is greatly appreciated.

Thanks guys!
-Nm

The function I am using is:

Function UnUrlSAFE (str)
While str <> &quot;&quot;
delimeter = instr(str,&quot;_SQUOTE_&quot;)
if delimeter = 0 then
if newStr <> &quot;&quot; then
newStr = newStr & &quot;'&quot; & str
else
newStr = str
end if
str = &quot;&quot;
elseif newStr <> &quot;&quot; then
newStr = newStr & &quot;'&quot; & mid(str,1,delimeter-1)
str = trim(mid(str,delimeter+8))
else
newStr = mid(str,1,delimeter-1)
str = trim(mid(str,delimeter+8))
end if
WEND
str = newStr
newStr = &quot;&quot;
While str <> &quot;&quot;
delimeter = instr(str,&quot;_AMPERSAND_&quot;)
if delimeter = 0 then
if newStr <> &quot;&quot; then
newStr = newStr & &quot;&&quot; & str
else
newStr = str
end if
str = &quot;&quot;
elseif newStr <> &quot;&quot; then
newStr = newStr & &quot;&&quot; & mid(str,1,delimeter-1)
str = trim(mid(str,delimeter+11))
else
newStr = mid(str,1,delimeter-1)
str = trim(mid(str,delimeter+11))
end if
WEND
UnUrlSAFE = newStr
End Function
 
before trying a example to fix the changed values, Why not just URLEncode the values before sending them

_____________________________________________________________________
onpnt2.gif

[bdaycandle]
 
there is a simpler way to do this
function UrlEncode(str)
{
return escape(str)
}

function UrlUnEncode(str)
{
return unescape(str)
}

on error goto hell
 
nested replace functions should be all you need to use.
returnValue = Replace(Replace(str,&quot;_SQUOTE_&quot;,&quot;'&quot;),_AMPERSAND_,&quot;&&quot;)


_____________________________________________________________________
onpnt2.gif

[bdaycandle]
 
sorry, the above method is for clientside javascript

for asp server side use the server.urlencode method to encode the special characters as mentioned by onpnt

on error goto hell
 
Beautiful. Thanks guys!

I knew there had to be something but some combination of friday and idiocy made it impossible for me to find on devguru.

I ended up just escaping it and handling it normally through the ASP page. Funny how there's always a better solution than massive functions on both ends...
 
Code:
I ended up just escaping it and handling it normally through the ASP page.  Funny how there's always a better solution than massive functions on both ends...

This sounds like what I need to do, but what does he mean by "just escaping it and handling it normally through the ASP page"

 
whew... kinda interesting that this thread is exactly a year old.

It was a long time ago, but the idea is that if you use the javascript method escape() or the ASP method server.URLEncode(), a string that goes into the URL will work correctly and display normally simply by using a

<%=Request.QueryString("MyVar")%>

So long as the place you are writing to is evaluated as HTML
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top