Gossamer Forum
Home : General : Perl Programming :

Remove any input spaces

Quote Reply
Remove any input spaces
I have two input text boxes. One named First Name and the other is named Last Name. How would I get it so that when I person typed in there whole name in the First Name by accident that it will remove the spaces between the name. So if they typed Mark Constant into the First Name fied it would come out as MarkConstant. Thanks in advance.
Quote Reply
Re: [constantm] Remove any input spaces In reply to
$FirstName =~ s/\s//g;

assumes you have $FirstName defined already of course. The \s is all whitespace characters.

--
Matt G
Quote Reply
Re: [Matt Glaspie] Remove any input spaces In reply to
You could try tr/// or y/// too....I've heard that is quicker than a regex substitution.

Last edited by:

Paul: Jun 22, 2002, 3:01 AM
Quote Reply
Re: [Paul] Remove any input spaces In reply to
Not as good as PHP..there is a function to remove front/end spaces..as well as all spaces in a variable, without having to use regex...heh heh Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Remove any input spaces In reply to
Yeah and what do you think is in that function ;)
Quote Reply
Re: [Paul] Remove any input spaces In reply to
I tried the s/\//g; but it still doesn't seem to do anything. Here is my parsing script. I am using the method GET if that makes a difference.

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@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{nd1} =~ s/\s//g;
$FORM{nd2} =~ s/\s//g;
$input{$name} = $value;
}
}

This edit.cgi is called from a script called add.cgi.
Quote Reply
Re: [constantm] Remove any input spaces In reply to
>> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

You're asking for someone to cause a buffer overflow... that above line is a well-known no-no.

Use CGI.pm instead of using the read-parse routine.
Quote Reply
Re: [Mark Badolato] Remove any input spaces In reply to
That's not going to be a problem in the traditional "buffer overflow" sense (a common c exploit), as perl dynamically grows scalars. CGI.pm has a POST_MAX option, but it defaults off so it's no safer. It's very hard to get a buffer overflow exploit in perl, as memory is managed for you.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [constantm] Remove any input spaces In reply to
In Reply To:
$FORM{nd1} =~ s/\s//g;
$FORM{nd2} =~ s/\s//g;
Where did these variables come from ($FORM{nd1} & $FORM{nd2})? What are their values before and after running the sub? I think in this case you want to use "$value" instead of $FORM{nd1} and $FORM{nd2}.
Matt G
Quote Reply
Re: [Matt Glaspie] Remove any input spaces In reply to
$FORM{nd1} and $FORM{nd2} are coming from another form called add.cgi. If I should use $value instead of $FORM{nd1} then where should I put it exactly in the form script? Also do you mean I should do $value{nd1}?
Quote Reply
Re: [Matt Glaspie] Remove any input spaces In reply to
I finally got it. Matt Glaspie got me thinking and I changed one line and it worked. Thanks to everybody that helped.
Quote Reply
Re: [constantm] Remove any input spaces In reply to
YW. Cool

You should post your revision so others can benefit from your solution should they be following the thread. Smile

--
Matt G
Quote Reply
Re: [Matt Glaspie] Remove any input spaces In reply to
I changed

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@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{nd1} =~ s/\s//g;
$FORM{nd2} =~ s/\s//g;
$input{$name} = $value;
}
}



to

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@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;
$input{$name} = $value;

$input{nd1} =~ s/\s+//g;

$input{nd2} =~ s/\s+//g;

}
}

So the only difference is that I used $input{nd1} instead of $FORM{nd1} and everything seemed to work after that. Thanks again for the tips.
Post deleted by Paul In reply to

Last edited by:

Paul: Jun 25, 2002, 9:16 AM
Quote Reply
Re: [constantm] Remove any input spaces In reply to
Hehe I'll try that again.

You have those regexs within the foreach loop which means they will run on every loop, you don't want that :)

You will probably want an if block to check if the current loop matches your field.

Last edited by:

Paul: Jun 25, 2002, 9:18 AM