Solved

more help with spaces, please

Posted on 2002-06-13
8
205 Views
Last Modified: 2010-03-05
Okay so this works:

$location =~ s/ /%20/g;

if I take it out, it doesn't work (in Netscape, but IE is fine).

However, it is replacing a single space with three '%20'

So that what should be "new york" comes out as "new%20%20%20york"

The script works nonetheless, but I think it looks messy.

Anyone know why it's doing this, and how I can fix it?

Thank you.

0
Comment
Question by:celere
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7076036
if it comes to url encoded string:

the standard doesn't allow to use space you should replace them with "+" sign or it's ascii number in hex.
space is ASCII 32 (in HEX it's  20) that's why it becomes %20.

this way you can encode other not allowed(or with special meaning as "/" ) chars - for example !@#$%^&*(),/:
  = ASCII 32 = %20
! = ASCII 33 = %21
" = ASCII 34 = %22
# = ASCII 35 = %23
and s.o. and s.o.

did you get the idea ?

0
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7076040
P.S if you want you can strip the aditional spaces and to use  +  it's more readable:

$location = "New   York"
$location =~ s/\s+/+/g;

#now $location is "New+York"
0
 
LVL 3

Accepted Solution

by:
Tsvetomir earned 50 total points
ID: 7076058
now I sow your other post probably the scripts misses somthing and multiplicates the chars by 3 each time .. I can't tell you where exactly is the mistace becouse I can't see the script here ... but the last probably  will solve the problem.
Beacouse it maj be used somewhere else as hex encoded you can use


$location =~ s/\s+/%20/g;



if this doesn't solve the problem you shold post the script here ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:celere
ID: 7076141
I will post the script shortly, but here is how it works thus far.

There is a menu of links with %20 hard coded in it:
<a ref'"http://mysite.com/cgi/read.cgi?newyork::New%20York">

This takes you to the "read.cgi" script, which delimits the :: as $shortname, $longname; opens up and reads $shortname.htm (newyork.htm), and places New York around the page to let the user know where they are.

$page=$ENV{'QUERY_STRING'};
$page=~s/^page=//;
$page =~ tr/ /%20/;
$newadtarget = "$page";
foreach($page)
     {
     ($shortname,$longname)=split(/::/,$_);
     }

Then there is a "post to this page" button (href, not form)
<a href="http://mysite.com/cgi/messages.cgi?$newadtarget">

which then goes to a second script for posting the messages. Here, NS is still reading only one %20 between New and York. After posting it returns to the same script for a "Thank You" page. This is the first occurence of the %20's tripling, as it reads "click here to see your ad" taking you back to <read.cgi..$shortname::$longname>

So I need $longname to be able to take one %20, not three, and I need it to be dynamic; I can't hardcode anything into it.

The message script also prints a link to the ad to appear among the "random message generator". It prints it with 3 %20's. So if someone were to click on that, and subsequently post an ad on the same page, it would print with 9 %20's.

I would like the most efficient way of doing this (keeping my scripts separate however, since they contain a lot of HTML). I will try your idea above, but if you think I should start over with the links, etc. and if you can help me clean it up (it is a little verbose!) I'll increase the points.

Thanks for pointing out I had two questions. I didn't post twice, I posted once and then "edited" it, but there should only be one.


0
 
LVL 10

Expert Comment

by:rj2
ID: 7076295
It's not only spaces that are now allowed in URL, there are other characters too. Sample below should get them all.

use URI::Escape;
$_="New     York";
s/ +/ /;
$_=uri_escape($_);
print "$_\n";
0
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7076674
????
ARE YOU shure its' like that ??? :
$page =~ tr/ /%20/;

this is a mistake ?!

it should be
$page =~ s/ /%20/g;
# or to have only 1 space:
$page =~ s/\s+/%20/;

tr replaces the chrars from left to their CORESPONDING chars on the rigth side 1 by 1 this means that
if it found space it will replace it with the coresponding char from right side and this is % not %20!

are u sure it's like that ??  I'm amazed that script works like that!

try this and see if it works ...


0
 

Author Comment

by:celere
ID: 7111444
I replaced all the spaces with +'s and now it works like a charm. Thank You!
0
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 7117940
you are wellcome :))
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question