Solved

CGI Trim

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

Is there a way to trim a stirng in CGI?

NeoTek
0
Comment
Question by:NeoTek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

732 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