CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
removing carriage returns and pipes from a form entry.

 



zipflip
Deleted

May 9, 2000, 12:01 PM

Post #1 of 10 (1698 views)
removing carriage returns and pipes from a form entry. Can't Post

i would appreciate any advice on this!...

my script allows users to post and modify entries in a unix flatline database with fields separated by pipes and entries separated by carriage returns. i would like to allow users to enter large textfields in some fields, but it causes problems when they hit return because the script then thinks that their entry is two entries.

i have tried using the following to remove pipes and returns, but it doesn't seem to work:

#################
foreach $field (@db_user_definable_field_order)
{
$field =~ s/\|//g; # removes pipes
$field =~ s/\n//g; # removes returns
}
#################


i have also tried this from another bb, and it doesn't work for me either:


########################
foreach $field (@db_user_definable_field_order)
{
$text=&remove_cr($form_data{'$field'});
$form_data{'$field'}=$text;
}

sub remove_cr
{
$TAB="<p>";
my($cheese)= @_;
$cheese =~ tr/\n\r\n/ /d;
return ($cheese);
}
###################################

does anyone have a better idea? or did i screw something up there?

thanks in advance....

kris

[This message has been edited by zipflip (edited 05-09-2000).]


Jasmine
Administrator / Moderator

May 9, 2000, 8:04 PM

Post #2 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

Try adding an m to the end of your regex:

$field =~ s/\|//mg; # removes pipes
$field =~ s/\n//mg; # removes returns

The m tells Perl that this field may span across multiple lines.




Cure
User

May 10, 2000, 1:21 PM

Post #3 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

Hi

NOTE:: The /m does NOT tells Perl that this field may span across multiple lines.

this will work just fine $field =~ s/\|//g; # removes pipes
$field =~ s/\n//g; # removes returns

The /m ONLY affects ^ and $

so using with m, ^ means at the beginning, or right after a \n

using m, $ means it will match the boundary between a character and the \n

example: print 1 if "abc\ndef" =~ /(abc$)\ndef/m; this would print 1


Cure



zipflip
Deleted

May 10, 2000, 1:44 PM

Post #4 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

jasmine and cure, thank you both for responding so quick. i really appreciate it. as a result of your responses i'm trying to stick to:

######################################################
foreach $field (@db_user_definable_field_order)
{
$field =~ s/\|//g; # removes pipes
$field =~ s/\n//g; # removes returns
}
######################################################

unfortunately, when i enter carriage returns into the form they still show up in the data file and it screws the whole database up.

i think the problem might lie in the somewhere else--the form input does not come in as an array, although the input fields do correspond to @db_user_definable_field_order.

i get form data in like this:

$form_data{'field_name_goes_here'}

so maybe the beginning of those lines is wrong. i have tried the following variations:

######################################################
foreach $field (@db_user_definable_field_order)
{
$form_data{$field} =~ s/\|//mg; # removes pipes
$form_data{$field} =~ s/\n//mg; # removes returns
}
######################################################
# or
######################################################
foreach $field (@db_user_definable_field_order)
{
$form_data{'$field'} =~ s/\|//mg; # removes pipes
$form_data{'$field'} =~ s/\n//mg; # removes returns
}
######################################################
# or
######################################################
foreach $field (@db_user_definable_field_order)
{
$field =~ s/\|//mg; # removes pipes
$field =~ s/\n//mg; # removes returns
}
######################################################

still, none of them work. i'm kinda lost. thanks in advance if you gots any suggestions.

kris


[This message has been edited by zipflip (edited 05-10-2000).]


Jasmine
Administrator / Moderator

May 10, 2000, 1:49 PM

Post #5 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

And so I stand corrected Smile

A blatant example of "never assume", especially when you have O'Reilly books hanging around Smile

[This message has been edited by Jasmine (edited 05-10-2000).]


Cure
User

May 10, 2000, 2:17 PM

Post #6 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

Hi zipflip

you are removing the new-line character from the database. a USER cannot enter a carriage returns in a text field(hint:: A new-line character is the enter key on your key board.

When a user enter this \n in a text field THAT IS NOT A NEW-LINE CHARACTER thats just a \n, so to get rid of \n then do this
$field =~ s/\\n//g; # \n

to get rid of \n and new-line character and pipes(|) do this-->
$field =~ s/(\|)|(\n)|(\\n)//g; # removes pipes,# removes returns, removes \n

or this way, your choice-->
$field =~ s/[\|\n\\n]//g; # removes pipes,# removes returns, removes \n


Cure




zipflip
Deleted

May 10, 2000, 2:40 PM

Post #7 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

(i really dig all these smiley icons)

thanks, cure. i'm sorry this problem is dragging on like this...

i should have been more accurate--the html form has some text areas (e.g. <textarea name="field_name"></textarea> ) that do allow the user to enter carriage returns. i'm not sure if it's called a new line or a carriage return but it does come from the user when they press the return or enter key in one of these text areas. i tried your suggestion but it still causes a problem. i have a feeling the regex is fine but something else is wrong, and i can't expect you to diagnose my entire script sight unseen.

i guess the only question i can think of is: do you have any general ideas of what might otherwise be wrong? the regex inquestion is located within the subroutine that writes to the database just before it uses the form data.

thank you so much for your help,
kris


zipflip
Deleted

May 10, 2000, 5:10 PM

Post #8 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

thank you so much i finally figured it out!

works fine.


japhy
Enthusiast

May 10, 2000, 5:56 PM

Post #9 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

Your main problem was you were using single quotes around a variable. You did

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$hash{'$var'}
</pre><HR></BLOCKQUOTE>

when you should have just done

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$hash{$var}
</pre><HR></BLOCKQUOTE>

That was (more or less) the cause of all your grief. If you want to remove all newlines and pipe signs from a variable, I suggest

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$scalar =~ tr/\n|//d;
$array[$index] =~ tr/\n|//d;
$hash{$key} =~ tr/\n|//d;
</pre><HR></BLOCKQUOTE>


zipflip
Deleted

May 11, 2000, 4:20 PM

Post #10 of 10 (1698 views)
Re: removing carriage returns and pipes from a form entry. [In reply to] Can't Post

i figured i should let you know... this regex removes all "n" characters from new entries (e.g. when someone types in "nifty" it prints "ifty" to the db):

$field =~ s/(\|)|(\n)|(\\n)//g; # removes pipes,# removes returns, removes \n

this is what i'm using now--it deletes all pipes and new lines but keeps the \n's without creating new lines in the database:

######################################################
foreach $field (@db_user_definable_field_order)
{
$form_data{$field} =~ s/(\|)|(\n)//g; # removes pipes, removes returns
}
######################################################

later,

kris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives