Replacing Ampertsand in cgi url

J

Jean

I have been trying this for hours without luck so far... Needless to
say my regular expression skills are bad...

I have some urls in an HTML page that have the amperstand sign (&) in
them, and I want to change only these amperstand into %26.

For example:

<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.

Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

I have tried 100s of variation on this:

$content =~ s/\s*=\s*[A-Za-z0-9]&/=$1%26/g;

But of course it does not work.

Any help would be greatly appreciated... thanks.

Jean
 
G

Gunnar Hjalmarsson

Jean said:
I have some urls in an HTML page that have the amperstand sign (&)
in them, and I want to change only these amperstand into %26.

For example:

<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.


Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

This might be what you want:

$content =~ s/(=[^\s&]*)&(?=\w+=)/$1%26/g;

Why do you want to do that substitution?
 
B

Brian McCauley

Jean said:
I have some urls in an HTML page that have the amperstand sign (&) in
them, and I want to change only these amperstand into %26.

For example:

<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.

Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

Not this thia has anything to do woith perl but I very much doubt you
actually want to do this. A %26 in a URL query string will not act as
an agrument delimiter - it will act as a literal ambersand in the data.

You want a literal ampersand in the URL which will be interpreted as a
delimiter. To get a literal ampersand in the URL then in HTML this must
be encoded as an entity &amp; (or a numeric entity if you prefer).

You probably should duck the whole issue an use a semi-colon instead.
 
J

Joe Smith

Jean said:
<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.

Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

No, it should not.

"cgi-bin/test.cgi?var1=1&var=2" should remain as is, or be converted to
"cgi-bin/test.cgi?var1=1;var=2", otherwise your URL won't work.

Do you, perhaps, have a beginner-written or buggy test.cgi?
-Joe
 
A

Alan J. Flavell

Not that it's a Perl issue, but...

No, it should not.
Agreed.

"cgi-bin/test.cgi?var1=1&var=2" should remain as is,

No! - making the obvious assumptions about context, if this variant is
used then the query string in the "URI attribute value" should read
var1=1&amp;var=2 , or its equivalent (&). See:

http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.2

(which re-iterates a piece of advice that was in RFC1866 already)
or be converted to "cgi-bin/test.cgi?var1=1;var=2",

Provided that the script supports this, yes (CGI.pm supports either
delimiter, as indeed it must if it's to handle real forms submission
as well as handling URLs composed according to B.2.2).
otherwise your URL won't work.

Well, href="cgi-bin/test.cgi?var1=1&var=2" might give an impression
of working, but it's still broken, and will cause the thing to
fail HTML syntax validation.

The amusing this about this (assuming HTML) is that it's precisely
when it -doesn't- fail syntax validation, that it stops even giving an
impression of working: my example would be ?print=yes&copy=3 (results
of actual browser tests are on a web page of mine, but that's OT
here).

regards
 
J

J. Romano

I have been trying this for hours without luck so far... Needless to
say my regular expression skills are bad...

I have some urls in an HTML page that have the amperstand sign (&) in
them, and I want to change only these amperstand into %26.

For example:

<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.

Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.


Dear Jean,

Try the regular expression s/\b(href="[^"]*)&/$1%26/i, with this
line:

1 while $content =~ s/\b(href="[^"]*)&/$1%26/i;

The regular expression will replace the first "&" symbol in an
href-quoted tag. The "while" loop exists in case there are more than
one "&" symbols in an href tag, like this:

<a href="test.cgi?var1=1&var2=2&var3=3">Bob & Paul</a> & Steve.

That way, it will keep looping until it has no more href "&" symbols
to replace (instead of stopping after the first one).

I hope this helps, Jean.

-- Jean-Luc
 
J

JR

I have been trying this for hours without luck so far... Needless to
say my regular expression skills are bad...

I have some urls in an HTML page that have the amperstand sign (&) in
them, and I want to change only these amperstand into %26.

For example:

<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> & Steve.

Should become:

<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

Jean

Jean,

A far less than perfect solution might be something such as the below
script. I'm sure someone will point out the problems with this
approach and provide a much better solution, but until then, this may
get you started. It at least appears to provide the correct output.

Good luck.

JR

#!/perl -w
use strict;

my ($hyper, $link) =
qq!<a href="cgi-bin/test.cgi?var1=1&var=2">Bob &amp; Paul</a> &
Steve.!
=~ /^(<.*?>)(.*)$/;

$hyper =~ s/&/%26/g;

print $hyper.$link;

=pod
actual output:
<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.

desired output:
<a href="cgi-bin/test.cgi?var1=1%26var=2">Bob &amp; Paul</a> & Steve.
=cut
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top