Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

URL Escaping

by electronicMacks (Beadle)
on Dec 28, 2000 at 03:25 UTC ( [id://48552]=perlquestion: print w/replies, xml ) Need Help??

electronicMacks has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks:

I’m writing a script that (among other things) converts strings to URL encoded format. I’ve been using uri_escape from URI::Escape which works great for some characters (like spaces) but does not, by default, escape characters such as & and ; although it is possile to turn on each of these characters individualy, I can't shake the feeling that there must be a better way...

So, my question is, can anyone recommend a good module that will encode all non alphanumeric characters? How about all but : and /

A humble monk,
electronicMacks

Replies are listed 'Best First'.
(Ovid encoding URLs)Re: URL Escaping
by Ovid (Cardinal) on Dec 28, 2000 at 03:47 UTC
    The function uri_escape takes an optional second argument that's a list of all characters that you want to encode. The following should work for you:
    #!/usr/bin/perl -w use strict; use URI::Escape; my $cgi_chars = '\x00-\x29\x2b\x2c\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\xff +'; my $bad_chars = '^%$&='; print uri_escape( $bad_chars, $cgi_chars );

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: URL Escaping
by eg (Friar) on Dec 28, 2000 at 03:33 UTC

    How about CGI::escape()? It translates everything except [a-zA-Z0-9_.-]. If it doesn't do what you want, you can always grab the four or five line definition out of CGI.pm and customize it for your needs.

Re: URL Escaping
by merlyn (Sage) on Dec 28, 2000 at 06:02 UTC
    It's not supposed to escape those because those are legal characters in a URL. (Didn't we do this one already?)

    There's nothing wrong with a URL of

    http://www.stonehenge.comm/cgi/foobar/fred&barney
    That doesn't mean that fred or barney are query parameters. The ampersand is only of some significance after the question mark.

    If you want to build a query string from pieces, use the proper part of URI... the parts with the word query in them.

    Stop trying to use the wrong tool for the wrong job in the wrong way.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://48552]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found