von dp am 23.August 98 um 22:11:35:
zu: Database in PERL von dp am 11.Februar 98 um 18:03:29:
From: jdowdell@macromedia.com (John Dowdell)
Subject: Re: mailto continued and important
Sender: owner-lingo@penworks.com
Here are two *rough* handlers for taking an arbitrary string and encoding
non-alphabetic characters so that it is acceptable as an URL, and the
reverse.
They're rough because I feel the responsibility to check this against
various RFCs to make sure edge conditions are handled correctly. My
character lists right now are just from books and I'm not certain I've
correctly handled all conditions.
Note too that most search engines like the "+" sign instead of "%20" when
term values are multiword. Ideally the following would be part of a routine
to turn a property list into CGI query term. Heck, this should actually be
part of a general CGI submission behavior, but simulating TEXTAREA and
IMAGE and other INPUT options is more than an afternoon's work. That's the
general direction though.
(In a nutshell, non-alphanumeric characters are hex-encoded in URLs, which
is why SPACE (ASCII 32) comes out as "%20" and so on.)
Corrections and complaints welcome, tia.
jd
on URLEncode aString
-- Rough early handler; use with caution. (jd 11/14/97)
set theUrl to ""
set legalChars to "abcdefghijklmnopqrstuvwxyz0123456789$-_.+"
set hex to "0123456789ABCDEF"
repeat with i = 1 to length(aString)
set thisChar to char i of aString
if legalChars contains thisChar then
set theUrl to theUrl & thisChar
else
set thisNum to charToNum(thisChar)
set highNibble to char (1 + thisNum / 16) of hex
set lowNibble to char (1 + thisNum mod 16) of hex
set theUrl to theUrl & "%" & highNibble & lowNibble
end if
end repeat
return theUrl
end
on URLDecode anUrl
-- Rough early handler; use with caution. (jd 11/14/97)
set theString to ""
set hex to "0123456789ABCDEF"
repeat while length(anUrl)
if char 1 of anUrl "%" then
set theString to theString & char 1 of anUrl
delete char 1 of anUrl
else
set highNibble to offset(char 2 of anUrl, hex) - 1
set lowNibble to offset(char 3 of anUrl, hex) - 1
set theString to theString & numToChar(16 * highNibble + lowNibble)
delete char 1 to 3 of anUrl
end if
end repeat
return theString
end
D. Plänitz