Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing spaces in perl with %20 for netscape

Status
Not open for further replies.

oshjosh

Technical User
Mar 11, 2002
34
0
0
TH
Hi I have a search program that searches using a "keyword" search parameter. Sometimes the keywords have a space.

My program starts like this.

#!/usr/bin/perl
use CGI;
$req = new CGI;
$keywordpara = $req->param('Keywords');
$datadir = "/home/open(FILE,"$datadir/data.log");
@data = <FILE>;
close(FILE);
print &quot;content-type:text/html\n\n&quot;;
$num = @data;
$result=0; $i=0; $j=0; $a=0;
foreach $data (@data) {
($id,$simage,$bimage,$artist,$ref,$size,$desc,$price,$title,$display,$Category,$Keywords) = split(/::/, $data);
if (($artist =~ /\b$keywordpara/i)||($title =~ /\b$keywordpara/i)) {
#if (($Category eq $categorypara)) {
$result++;
}
}
$page = $req->param('page');
if ($page eq '') { $page=0;}
else { $line = $page*6; }


Then prints the data depending on the keywords using this.

foreach $data (@data) {
($id,$simage,$bimage,$artist,$ref,$size,$desc,$price,$title,$display,$Category,$Keywords) = split(/::/, $data);
if (($artist =~ /\b$keywordpara/i)||($title =~ /\b$keywordpara/i)) {
$a++;
if ($a <= $line) {
next;
}
else {
$i++;$j++;
if ($j <= 6) {
if ($i==1) {
print &quot;<tr>&quot;;
}
etc...

It works fine for the first search as I have replaced all the spaces in my links with %20

However at the end of the program I have a link to next page section:

print &quot;<CENTER><table><tr><td><span class=\&quot;title\&quot;>page - </span>   &quot;;
$num_of_page = $result/6;
for($b=0;$b<$num_of_page;$b++)
{
$view = $b+1;
print &quot;<span class=\&quot;cgifoot\&quot;><a class=\&quot;cgilink\&quot; href=\&quot;}
print &quot;</span></td></tr></table></CENTER>&quot;;

The problem is that in Netscape even when the URL has the spaces replaced in the keywords with %20 the link to page part of the cgi program (search.cgi?page=$b&Keywords=$keywordpara) is not picking this up and therefore is not displaying the next page results in Netscape.

I'm not very experienced in perl so simple answers would be much appreciated..

Thanks in advance.. Rob
 
To replace all spaces with &quot;%20&quot; you would do

my $string = &quot;test of replace&quot;
$string =~ s/ /%20/g

However, there are several other characters that have to be escaped if ther are to be sent in paremeters through a URL (such as &quot;?&quot;, &quot;;&quot; or &quot;&&quot;). My guess is there is a function to handle this sort of escaping in the CGI package.
 
Thanks for taking the time to respond however where would I put that? my perl knowledge wont even take me that far I am afraid..

Thanks again..
Rob
 
I'm not sure I've gotten your code correctly, but I think you would want it in the last for-loop. just before printing the URL. Do it on all the variables you use for constructing the URL, ie $b and $keywordpara.

That should do it.
 
Hi abbeman

I have pasted the whole script below in the hope you can help further.. Ive added in as you said at the bottom on the loop just before printing the URL, however netscape is still showing the URL with spaces and gives it as a bad reqest.

Example..

works Ok as the link has the space replaced with a + however at the bottom of the page the next page links are still using a space rather than replacing it with %20

Thanks if you can help

Rob

#!/usr/bin/perl
use CGI;
$req = new CGI;
$keywordpara = $req->param('Keywords');
$datadir = &quot;/home/oshjosh/abacus-gallery.com/open(FILE,&quot;$datadir/data.log&quot;);
@data = <FILE>;
close(FILE);
print &quot;content-type:text/html\n\n&quot;;
$num = @data;
$result=0; $i=0; $j=0; $a=0;
foreach $data (@data) {
($id,$simage,$bimage,$artist,$ref,$size,$desc,$price,$title,$display,$Category,$Keywords) = split(/::/, $data);
if (($artist =~ /\b$keywordpara/i)||($title =~ /\b$keywordpara/i)) {
#if (($Category eq $categorypara)) {
$result++;
}
}
$page = $req->param('page');
if ($page eq '') { $page=0;}
else { $line = $page*6; }
######header########
open(FILE,&quot;top.txt&quot;);
@header = <FILE>;
close(FILE);
foreach $header (@header) {
print &quot;$header&quot;;
}
#####3##############
print &quot;<table><tr><td width=\&quot;39\&quot; valign=\&quot;top\&quot; align=\&quot;left\&quot;></td><td width=\&quot;409\&quot; valign=\&quot;top\&quot; align=\&quot;left\&quot;><span class=\&quot;title\&quot;>$keywordpara<br> </span><img src=\&quot; width=\&quot;260\&quot; height=\&quot;11\&quot;></td><td width=\&quot;326\&quot; valign=\&quot;top\&quot; align=\&quot;left\&quot;></td></tr></table><br><br>&quot;;

#start Bonnard Testimonial
if ($keywordpara eq &quot;Pierre Bonnard&quot;)
{
print &quot;<center>\&quot;Thank you so much for a wonderful painting. Bonnard is my favorite artist and now I own a perfect reproduction.<br> It arrived on time just as you promised.\&quot; <i><font color=\&quot;#FF0000\&quot;>Mario Schiavonne, Milan, Italy</font></i>.</center><br><br>&quot;;
}
#end changes Bonnard Testimonial

#start Monet Testimonial
if ($keywordpara eq &quot;Claude Monet&quot;)
{
print &quot;<center>\&quot;My designer recommended you and I am so glad she did. The Monet painting is fabulous <br>and I will for sure be ordering another very shortly. A fantastic painting! Thank you.\&quot; <i><font color=\&quot;#FF0000\&quot;>Betsey Drover, Michigan Il</font></i>.</center><br><br>&quot;;
}
#end changes Monet Testimonial

#start Warhol Testimonial
if ($keywordpara eq &quot;Andy Warhol&quot;)
{
print &quot;<center>\&quot;My wife and I were truly delighted to receive our \&quot;Warhol Shoes\&quot; from you. <br>It is an excellent reproduction for which we are most grateful. \&quot; <i><font color=\&quot;#FF0000\&quot;>Robert and Sarah Mullen, Leicester. UK</font></i>.</center><br><br>&quot;;
}
#end changes Warhol Testimonial

#start Edward Hopper Testimonial
if ($keywordpara eq &quot;Edward Hopper&quot;)
{
print &quot;<center>\&quot;My painting arrived this morning. You have captured the light perfectly; it's an excellent piece of work.<br> Thank you so much. \&quot; <i><font color=\&quot;#FF0000\&quot;>Abrams, Charleston. (Hopper's Sunday Morning)</font></i>.</center><br><br>&quot;;
}
#end Edward Hopper Testimonial

#start Paul Gauguin Testimonial
if ($keywordpara eq &quot;Paul Gauguin&quot;)
{
print &quot;<center>\&quot;Dear Sirs, A few months back I ordered Tahitian Women by Paul Gauguin and I now wish to order Tahitian Mountains as I am so pleased with the first one. I look forward to receiving this painting soon. \&quot; <i><font color=\&quot;#FF0000\&quot;>Fiona McNally, Fulham, London</font></i>.</center><br><br>&quot;;
}
#end Paul Gauguin Testimonial

#start Tamara de Lempicka Testimonial
if ($keywordpara eq &quot;Tamara de Lempicka&quot;)
{
print &quot;<center>\&quot;Hi Katie, I just received my Auto-Portrait reproduction of Tamara de Lempicka's work. How do you do it? It's amazing, you've been so sweet and patient answering all my questions and I am thrilled with the result. Thank you again. \&quot; <i><font color=\&quot;#FF0000\&quot;>Timothy Charlston, Sacramento</font></i>.</center><br><br>&quot;;
}
#end Tamara de Lempicka Testimonial

#start Picasso Testimonial
if ($keywordpara eq &quot;Pablo Picasso&quot;)
{
print &quot;<center>\&quot;Hello, My fabulous painting arrived yesterday. I can't wait to get it to the framers and up on the wall. It just adds the finishing touches to my new home. Could you do another Picasso for me? The one I want isn't shown but I can give you all the details? \&quot; <i><font color=\&quot;#FF0000\&quot;>Paul and Jackie Lo, Florida</font></i>.</center><br><br>&quot;;
}
#end Picasso Testimonial

#start Monet Testimonial
if ($keywordpara eq &quot;Claude Monet&quot;)
{
print &quot;<center>\&quot;Great Service and Great Painting. Keep up the good work. \&quot; <i><font color=\&quot;#FF0000\&quot;>Cherie Marsden, Nairobi. (Monet and van Gogh)</font></i>.</center><br><br>&quot;;
}
#end Monet Testimonial

#start Van Gogh Testimonial
if ($keywordpara eq &quot;Vincent van Gogh&quot;)
{
print &quot;<center>\&quot;Great Service and Great Painting. Keep up the good work. \&quot; <i><font color=\&quot;#FF0000\&quot;>Cherie Marsden, Nairobi. (Monet and van Gogh)</font></i>.</center><br><br>&quot;;
}
#end Van Gogh Testimonial

#start Matisse Testimonial
if ($keywordpara eq &quot;Henri Matisse&quot;)
{
print &quot;<center>\&quot;I had to write to congratulate you on the service you offer. My painting (Matisse 'The Dream') arrived was so beautifully packaged and it arrived just on time as you promised it would. I am sure to be in contacting you again.\&quot; <i><font color=\&quot;#FF0000\&quot;>Jerome Nibbs, Southampton UK</font></i>.</center><br><br>&quot;;
}
#end Matisse Testimonial

#start Klimt Testimonial
if ($keywordpara eq &quot;Gustav Klimt&quot;)
{
print &quot;<center>\&quot;My wife has always admired the work of Klimt and The Kiss is her favourites. I am so pleased to have been recommended to you by a friend and chosen this painting for our 10th Wedding anniversary. She is delighted with the present and it will give us both much pleasure over the next 10 years.\&quot; <i><font color=\&quot;#FF0000\&quot;>Sincerely, Joshua Cohen, Hampstead, London</font></i>.</center><br><br>&quot;;
}
#end Klimt Testimonial

print &quot;<table cellpadding=0 cellspacing=0 width=100%>&quot;;


foreach $data (@data) {
($id,$simage,$bimage,$artist,$ref,$size,$desc,$price,$title,$display,$Category,$Keywords) = split(/::/, $data);
if (($artist =~ /\b$keywordpara/i)||($title =~ /\b$keywordpara/i)) {
#if (($Medium eq $mediumpara) && ($Category eq $categorypara)) {
$a++;
if ($a <= $line) {
next;
}
else {
$i++;$j++;
if ($j <= 6) {
if ($i==1) {
print &quot;<tr>&quot;;
}
print&quot;<td width=30% align=center valign=top>&quot;;
print &quot;<table border=\&quot;1\&quot; cellpadding=\&quot;0\&quot; cellspacing=\&quot;0\&quot; bordercolor=\&quot;#727272\&quot; bordercolorlight=\&quot;#727272\&quot; bordercolordark=\&quot;#727272\&quot;><tr><td><img src=\&quot;$simage\&quot; alt=\&quot;$artist $title\&quot;></a></td></tr></table><br><span class=\&quot;cgi\&quot;><b>\$$price<br><span class=\&quot;cgi\&quot;><b>$artist</b><br>$title</b><br>$size<br><a class=\&quot;cgilink\&quot; href=\&quot; this Painting<a/><br><br>&quot;;

if ($poster eq &quot;1&quot;)
{
print &quot;or<br><a class=\&quot;cgilink\&quot; href=\&quot; poster\&quot;>Order a Color Poster</a> <br>$Postersize,\$$posterprice<br>&quot;;
}
print &quot;</td>&quot;;
if ($i == 3) {
print &quot;</tr><tr height=\&quot;22\&quot;></tr>&quot;;
$i = 0;
}
} # end j <=6
else { last;}
} # end show
} # end if

} # end foreach
print &quot;</table>&quot;;

print &quot;<CENTER><table><tr><td><span class=\&quot;title\&quot;>page - </span>   &quot;;
$num_of_page = $result/6;
for($b=0;$b<$num_of_page;$b++)
{
$view = $b+1;
my $string = &quot;test of replace&quot;
$string =~ s/ /%20/g
print &quot;<span class=\&quot;cgifoot\&quot;><a class=\&quot;cgilink\&quot; href=\&quot;}
print &quot;</span></td></tr></table></CENTER>&quot;;
#### footer #######
open(FILE,&quot;bottom.txt&quot;);
@footer = <FILE>;
close(FILE);
foreach $footer (@footer) {
print &quot;$footer&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top