Solved

CGI Trim

Posted on 2004-04-19
17
797 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
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!

 
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

Independent Software Vendors: 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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
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…

691 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