Re: & url question
by davidrw (Prior) on May 24, 2005 at 13:26 UTC
|
The & needs escaping as %26 like this:
mycode.pl?param=first&second=name&details=items%26things&other=more<br
+>
There are modules to escape stuff for you. For example, URI::Escape -- this example is basically straight from the man page:
perl -MURI::Escape -le 'print uri_escape("10% is enough\n");
# 10%25%20is%20enough%0A
| [reply] [d/l] [select] |
Re: & url question
by holli (Abbot) on May 24, 2005 at 13:33 UTC
|
Because "&" is special in URLs you have to encode it:
use Tie::UrlEncoder;
print "mycode.pl?param=first&second=name&details=" .
$urlencode{"items&things"} .
"&other=more";
#mycode.pl?param=first&second=name&details=items%26things&other=more
| [reply] [d/l] |
Re: & url question
by reasonablekeith (Deacon) on May 24, 2005 at 13:18 UTC
|
call your parameter "items_and_things"?
Update:
This is the wrong way round. You want to know how to pass an ampersand on a url. You need to escape it.
& => %26
---
my name's not Keith, and I'm not reasonable.
| [reply] |
|
|
Thanks,
Here is what I did,
$url =~ s/&/%26/g;
And it worked, it was a great idea!
Thank you! | [reply] [d/l] |
Re: & url question
by rlucas (Scribe) on May 24, 2005 at 13:32 UTC
|
If you want your code to play nice with others, you should definitely:
- When constructing URL strings:
- Use only \w+ for parameter names
- uri-escape (see URI::Escape) the parameter values
- When parsing URL strings:
- Just use CGI.pm's param functionality. Or, for an extra buffer, use CGI::Safe.
The only "&" characters in your URL should be explicitly put there by you (or code that constructs the URL for you) as separators -- generally, all metacharacters, whitespace, etc. will be URI-escaped into %xy hex values. | [reply] |
|
|
Yes, I know but that name is a name that is like that, I can't change it.
| [reply] |
|
|
You aren't changing it. You are representing it in a URL.
In morse code the data "s" is represented as "...".
In URLs, the data "&" is represented as "%26"
Edit: Having reread the grandparent I now see I'm being stupid. The grandparent is good style advice, but sometimes changing things after the fact is a little difficult - especially on a short time scale. You can encode the names as well as the values of parameters though.
| [reply] |
Re: & url question
by saberworks (Curate) on May 24, 2005 at 17:44 UTC
|
Just for the record, if you want your XHTML to validate, you can't just use & in linked urls. You have to actually use & or the w3c validator complains. So if you are linking a url like this: http://domain/link.cgi?id=5&other_var=9, to get it to validate you have to do: http://domain/link.cgi?id=5&other_var=9. | [reply] [d/l] [select] |
|
|
Not exactly right. You have to write &-s as &-s in (X)HTML _only_. It's another coding: HTML parameter value coding.
<a href="index.cgi?name=AT%26T&id=1">...</a>
The value of the link's href parameter (the URL, what you will see in the browser's location bar) will be "index.cgi?name=AT%26T&id=1", the value of name will be AT&T, and the value of id will be 1.
| [reply] |