Solved

CGI Trim

Posted on 2004-04-19
17
780 Views
Last Modified: 2013-12-25
Hi,

Is there a way to trim a stirng in CGI?

NeoTek
0
Comment
Question by:NeoTek
  • 5
  • 5
  • 4
  • +1
17 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 10860670
Define "Trim". Do you mean to remove white space at the front and back?

What language are you writing the CGI script in? Perl? Python? Ruby? PHP? C?

I'll assume Perl as it is most common.

$foo = "  hello   ";
$foo =~ /^\s*//;
$foo =~ /`s*$//;

should do the job IIRC.
0
 
LVL 1

Author Comment

by:NeoTek
ID: 10862565
Hi,

I want to trim the first 21 characters of a string (not just white space).  I am using Perl.  Could you also please explain?  Thanks.

NeoTek
0
 
LVL 17

Expert Comment

by:dorward
ID: 10862716
$foo = "abcdefghijklmnopqrstuvwxyz";
$foo = substr $foo, 21;
print $foo;

Substring is pretty self-explanitory. perldoc -f substr explainsin detail.
I don't think I can really add to that.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10865447
As always in Perl, many ways to achieve the same result:

 $foo = "abcdefghijklmnopqrstuvwxyz";
 $foo =~ s/^.{21}//;
 print $foo;
0
 
LVL 1

Author Comment

by:NeoTek
ID: 10867997
Hi,

Here is a part of my code.  I basically want to shorten the title (or the text the link displays) by 21 characters.  How can I do this?  I tried both of your suggestions, and I got errors.  Help!

sub return_html {
   print "Content-type: text/html\n\n";
   print "<html>\n <head>\n <script language='javascript'>self.resizeTo(300,300);window.moveTo(0,0);</script> <title>Search Results</title>\n </head>\n";
   print "<body>\n <center>\n <font color='095484' size='+2'>Search Results</font>:\n </center>\n";
   print "<hr size=7 width=100%>\n";
   print "<ul>\n";
   foreach $key (keys %include) {
      if ($include{$key} eq 'yes') {
         print "<li><a target='main' href=\"$baseurl$key\">$titles{$key}</a>\n";
      }
   }
   print "</ul>\n";
   print "<hr size=7 width=100%>\n";
   print "</body>\n</html>\n";
}

NeoTek
0
 
LVL 17

Expert Comment

by:dorward
ID: 10868028
It sounds like you want to reduce the string TO 21 characters rather then BY 21 characters.

     if ($include{$key} eq 'yes') {
         my $title = substr $titles{$key}, 0, 21;
         print "<li><a target='main' href=\"$baseurl$key\">$title</a>\n";
      }
0
 
LVL 1

Author Comment

by:NeoTek
ID: 10871000
HI,

I actually want to reduce the string BY 21 characters (the first 21).  Here is my code...

sub return_html {
   print "Content-type: text/html\n\n";
   print "<html>\n <head>\n <script language='javascript'>self.resizeTo(300,300);window.moveTo(0,0);</script> <title>Search Results</title>\n </head>\n";
   print "<body>\n <center>\n <font color='095484' size='+2'>Search Results</font>:\n </center>\n";
   print "<hr size=7 width=100%>\n";
   print "<ul>\n";
   foreach $key (keys %include) {
      $title = substr $titles{$key},21;
      if ($include{$key} eq 'yes') {
         print "<li><a target='main' href=\"$baseurl$key\">$title</a>\n";
      }
   }
   print "</ul>\n";
   print "<hr size=7 width=100%>\n";
   print "</body>\n</html>\n";
}


I get a 500 error when I try to execute it.

Ryan
0
 
LVL 17

Expert Comment

by:dorward
ID: 10871419
Then:
     if ($include{$key} eq 'yes') {
         my $title = substr $titles{$key}, 21;
         print "<li><a target='main' href=\"$baseurl$key\">$title</a>\n";
      }

should work. A 500 error just means "The script game an error so the server can't give you the page". It doesn't say anything about what that error is.

Look in the server log files to find out.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Expert Comment

by:Tintin
ID: 10874460
Add a

use CGI::Carp qw(fatalsToBrowser);

line at the top of your script to see the actual error.
0
 
LVL 3

Expert Comment

by:cjmos
ID: 10874487
Also, just to be safe

if (($include{$key} eq 'yes') && (defined $titles{$key})){
     my $title = substr $titles{$key}, 21;
     print "<li><a target='main' href=\"$baseurl$key\">$title</a>\n";
}

Otherwise if $titles{$key} wasn't defined you'd get an undefined variable error.
0
 
LVL 1

Author Comment

by:NeoTek
ID: 10877317
Hi,

It's still not working.  Here is the code...

CGI::Carp qw(fatalsToBrowser);
#!/usr/local/bin/perl
##############################################################################
# Simple Search                 Version 1.0                                  #
# Copyright 1996 Matt Wright    mattw@scriptarchive.com                      #
# Created 12/16/95              Last Modified 12/16/95                       #
# Scripts Archive at:           http://www.scriptarchive.com/                #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
#                                                                            #
# Simple Search may be used and modified free of charge by anyone so long as #
# this copyright notice and the comments above remain intact.  By using this #
# code you agree to indemnify Matthew M. Wright from any liability that      #  
# might arise from it's use.                                                 #  
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  In other words, please ask first before you try and  #
# make money off of my program.                                              #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium.  In all cases copyright and header must remain intact.#
##############################################################################
# Define Variables                                               #

$basedir = '../www/';
$baseurl = 'http://www.airsep.com/';
@files = ('development/*.html','development/medical/*.html','development/corporate/*.html','airsepcpd/*.html');
$title = "AirSep Corporation";
$title_url = 'http://www.airsep.com/';
$search_url = 'http://www.airsep.com/development/search.html';
@blocked = ('/development/medical/confirmcontact.html','/development/commercial/*.html');

# Done                                                           #
##############################################################################

# Parse Form Search Information
&parse_form;

# Get Files To Search Through
&get_files;

# Search the files
&search;

# Print Results of Search
&return_html;


sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
   }
}

sub get_files {

   chdir($basedir);
   foreach $file (@files) {
      $ls = `ls $file`;
      @ls = split(/\s+/,$ls);
      foreach $temp_file (@ls) {
         if (-d $file) {
            $filename = "$file$temp_file";
            if (-T $filename) {
               push(@FILES,$filename);
            }
         }
         elsif (-T $temp_file) {
            push(@FILES,$temp_file);
         }
      }
   }
}

sub search {

   @terms = split(/\s+/, $FORM{'terms'});

   foreach $FILE (@FILES) {

      open(FILE,"$FILE");
      @LINES = <FILE>;
      close(FILE);

      $string = join(' ',@LINES);
      $string =~ s/\n//g;
      if ($FORM{'boolean'} eq 'AND') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if (!($string =~ /$term/i)) {
                  $include{$FILE} = 'no';
                last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if (!($string =~ /$term/)) {
                  $include{$FILE} = 'no';
                  last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
         }
      }
      elsif ($FORM{'boolean'} eq 'OR') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if ($string =~ /$term/i) {
                  $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if ($string =~ /$term/) {
              $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
         }
      }
      if ($string =~ /<title>(.*)<\/title>/i) {
         $titles{$FILE} = "$1";
         $titles($FILE) = substr $titles($FILE), 21;
      }
      else {
         $titles{$FILE} = "$FILE";
      }
   }
}
     
sub return_html {
   print "Content-type: text/html\n\n";
   print "<html>\n <head>\n <script language='javascript'>self.resizeTo(300,300);window.moveTo(0,0);</script> <title>Search Results</title>\n </head>\n";
   print "<body>\n <center>\n <font color='095484' size='+2'>Search Results</font>:\n </center>\n";
   print "<hr size=7 width=100%>\n";
   print "<ul>\n";
   foreach $key (keys %include) {
      if ($include{$key} eq 'yes') {
         my $title = substr $titles{$key},21;
         print "<li><a target='main' href=\"$baseurl$key\">$title</a>\n";
      }
   }
   print "</ul>\n";
   print "<hr size=7 width=100%>\n";
   print "</body>\n</html>\n";
}
   
0
 
LVL 17

Expert Comment

by:dorward
ID: 10877347
But what error does it record in your server logs when you try to run it?
0
 
LVL 3

Accepted Solution

by:
cjmos earned 125 total points
ID: 10877365
Chnage the line:

if ($include{$key} eq 'yes') {
to:
if (($include{$key} eq 'yes') && (defined $titles{$key})){

and
CGI::Carp qw(fatalsToBrowser);
to:
use CGI::Carp qw(fatalsToBrowser);

Then tell us the error you get in your browser if there still is one.
0
 
LVL 3

Expert Comment

by:cjmos
ID: 10877397
Also put

use CGI::Carp qw(fatalsToBrowser);

after #!/usr/local/bin/perl

0
 
LVL 1

Author Comment

by:NeoTek
ID: 10877501
Here is my error:

Software error:
Execution of airsepsearch.cgi aborted due to compilation errors.
Please send mail to this site's webmaster for help.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10883177
Argghhhh!  You've made a big mistake an installed one of Matt Wright's scripts.  Bad move.  They are very poorly written, insecure and are not maintained.

Go to http://nms-cgi.sourceforge.net/scripts.shtml  and download the equivalent search script there (one that is well written, secure and maintained).
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11147884
I *really* hope you did get rid of Matt's version and installed the NMS version.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now