?
Solved

Form question

Posted on 2003-03-12
19
Medium Priority
?
208 Views
Last Modified: 2010-03-05
I have a textarea on my page and I need to take whatever information is entered in and convert it so that the value of the textarea is one continuous string even if there are line breaks.

What is the best way to examine the string, find the line break and replace it with something like '#ln' so that i can keep track of where the line break was for when I output back to the form.
0
Comment
Question by:Bustarooms
[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
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 2

Expert Comment

by:linxit
ID: 8121004
For a start use the attribute WRAP="physical" to ensure that all line wraps are sent as linebreaks, so the string already contains where each linebreak was.

Then, assuming you're using CGI.pm

$mystr = params('mytextarea');

$mystr =~ s/\n//gm;

So now params('mytextarea') has the string as entered with linebreaks intact, $mystr has no linebreaks.
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8121437
can i replace those line breaks with another set of characters?
0
 

Expert Comment

by:frankja
ID: 8121496
something else you can do -
also assuming CGI.pm

$text_area = params('formtextarea');
@lines = split(/\n/,$text_area);
for(@lines)
{
  $formatted_str = "$_#ln";
}

That'll give you what you want i think. Note that there'll be an #ln at the end of the string too - maybe you want it there, maybe not. Your call =)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:Bustarooms
ID: 8121506
can i replace those line breaks with another set of characters?
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8121517
disregard my last comment
0
 

Expert Comment

by:frankja
ID: 8121557
something else you can do -
also assuming CGI.pm

$text_area = params('formtextarea');
@lines = split(/\n/,$text_area);
for(@lines)
{
  $formatted_str = "$_#ln";
}

That'll give you what you want i think. Note that there'll be an #ln at the end of the string too - maybe you want it there, maybe not. Your call =)
0
 

Expert Comment

by:frankja
ID: 8121565
oops - disregard the re-post. Accidentally hit the reload button. Sorry! =)
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8122099
It is still not working out quite right for me.

I have a text area and I am saving the information from this textarea into a file.

Fairly simple.  However, the problem is that if a user presses enter I do not want the file to be saved as:

"User typed
this"

rather it should be

"User type\nthis"

it doesn't even have to be \n it could be anything as long as i can search for it because I want to display it with the carriage returns later on
0
 

Expert Comment

by:frankja
ID: 8122247
gah - my fault

change this line:

  $formatted_str = "$_#ln";

to this line

  $formatted_str = $formatted_str."$_#ln";


now it should work for you. Sorry about that
0
 

Expert Comment

by:frankja
ID: 8122304
just to clarify the above, what i'm doing is splitting the full string at each new line. Then for each element in the array the split produces, I'm tacking on the next element and adding a #ln. So the substring you'd look for to put \n's back in would be #ln. Again, note that the #ln will be on the end of the string. If you don't want it there, you can just do

$formatted_str = substr($formatted_str, 0, length($formatted_str-3));

I know for a fact there is a better way to do this, but i can't remember for the life of me what it is. This isn't the prettiest code, but it gets the job done =)
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 8123055
Bustarooms -

The answer is yes, you can change it to another string, and the easiest method is to modify linxit's example slightly.  The following would put in your #ln:

use CGI qw(:standard);

$mystr = params('mytextarea');
$mystr =~ s/\n/\#ln/g;

It's more efficient to use the global search and replace than the looping algorithm.

In the past, I've used similar replacements for newlines - my working version used ~!~ as the replacement:

$string=~s/\n/\~\!\~/g;

0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8193717
Hi Bustarooms,
If you save whatever-enterred from a textarea to a file as-is and then latter output it without any change back to textarea, it will be whatever you want.

For example:
If your user enterred:
bla bla bla 1
another bla

You save this to file, and latter read it back, write to the textarea. It will be:
bla bla bla 1
another bla

exactly what you want.

The problem you are trying to overcome here is that (I guess) you want to convert text from multi-line to single-line? There is no need to do that because you can reread the whole file latter (I guest when you reread the file, you just read the fist line and you thought there was a bug in your code. Am I right?)
You can reread all your text in file as simple as this:
open(FILE, "filename.txt") or die "cannot open filename.txt";
my @data = <FILE>;
close(FILE);

Obviously you need to change filename.txt into your actual file's name.
Moreover, you can join all read lines into a single line like this:
my $single_line = join('', @data);

Hope this help.
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8198201
Thanks for everyone's help.  I am still having a bit of trouble getting it just right.  This is my fault because I have not been clear in what I am trying to do.

I am saving the form information into a file.  I have 5 different form elements one of which is a textarea.  I am taking whatever is entered in these forms and saving them to a file.  

The problem is occurring if a user hits enter in the textarea.  When I write the form information to the file, it see's this hard return and jumps down to the next line in the file.  This is bad because when I go to use this information, it thinks this line is a new record.  (ie. I type in:
line1
lin2

in my file it will read:
element1|element2|element3|element4|line1
line2|

I got it to "replace" the \n with ~!~ but for some reason it is still jumping to the next line when it writes the file.  it now reads as follows:

element1|element2|element3|element4|line1
~!~line2|

I have no idea why that is happening.

This is what I am doing

$notes = param("selNotes");
$notes =~s/\n/\~\!\~/g;

and later on when i write the file i simply
print writeDAT "$notes";

is $notes =~s/\n/\~\!\~/g; not actually replacing the \n just adding next to it?
0
 
LVL 15

Accepted Solution

by:
periwinkle earned 200 total points
ID: 8198608
Do this as well after the substitution:

$notes =~s/\r\\/g;

that will remove any line returns that might be there as well.
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8202744
YES it finally worked.  I used $notes =~s/\r//g; to get rid of those extra line breaks.  periwinkle i think you mistyped those slashes.

Thanks to all the experts!!

I am a bit of a newbie at this.  First question I posted.  How should I award these points since it was a bit of a combined effort, or do you guys really not care?
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8202765
Hi Bustarooms,
It's your choice. I myself won't complain at all :-)

Best wishes!
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 8202775
Bustarooms -

I certainly did mistype!  Sorry about that!

If you want to divide up points, you would post a Note in the community support area.  They will reduce the points for this one, and you'll post a second question in this area that says something like:

For <username>

and generally you would include a pointer to the problem within like:

For Problem 20548015

or better yet, the actual URL, like:

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20548015.html
0
 
LVL 10

Author Comment

by:Bustarooms
ID: 8202794
I used periwinkles help the most out of everyone.  Yeah periwinkle.
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 8202847
Thanks Bustarooms - glad to have helped.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

741 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