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: Advanced:
Text control for db

 



skirrow
stranger

Apr 15, 2001, 3:34 PM

Post #1 of 6 (1344 views)
Text control for db Can't Post

Hi,
I'm in the process of writing a set of scripts for a new project I'm working on. Mostly Perl, bit of PHP later on.
I've got a bit of a problem though. I'm using flatfile databases and I'm taking information from a form and recording it to a database.
Right, one of the fiels is a text area. Whenever I enter anything, it seems to print it in the db as it appears in the field. I'm pretty sure it's possible to do the following but am not sure how so any help would be very useful.
I need to find a way of squashing in effect the entries into the text box. Simply flattening them into one line.
So rather than looking like:
The
Dog
Went to the
Park
It would automatically make it look like:
The Dog Went to the Park

Any ideas how to do this?

Best Regards
- Neil

--
http://www.cgi-index.com/
http://www.cgiscriptreview.com/


Jean
User


Apr 16, 2001, 12:42 AM

Post #2 of 6 (1338 views)
Re: Text control for db [In reply to] Can't Post

Basically you can do it in the following way,
though the example is very simple and most
of the chances you'll have to improve the expression.


Code
my $str = "The 
Dog
Went to the
Park";

$str =~ s/\n/ /g; # Replace new line characters with space
print "$str\n";

Hope it helps ...

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


skirrow
stranger

Apr 16, 2001, 12:50 AM

Post #3 of 6 (1337 views)
Re: Text control for db [In reply to] Can't Post

I tried that last night and again today and it doesn't work. It still splits it up on to different lines. Basically prints it in the database how it looks in the text area in the form when I type it!

- Neil

--
http://www.cgi-index.com/
http://www.cgiscriptreview.com/


Jean
User


Apr 16, 2001, 2:26 AM

Post #4 of 6 (1335 views)
Re: Text control for db [In reply to] Can't Post

It worked for me on both Solaris and Win2K, though I'm not using HTML, CGI etc.
Maybe your lines contain "\r" instead of or in addition to the "\n" ...


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


BigRich
Novice

Apr 26, 2001, 4:25 AM

Post #5 of 6 (1311 views)
Re: Text control for db [In reply to] Can't Post

First, make sure that your textarea tag has the "wrap" attribute set to "virtual" not "physical". (<textarea name="mytextarea" rows =5 cols=40 wrap="virtual">)

Then remove any user entered "returns" and replace them with a space before printing it to the db.

$textarea_in = $q->param('mytextarea');

$textare_in =~ s/(\r*\n)+/ /g;

Now you should be able to print the input to the db without the returns.

You'll lose the formatting if you plan to print it back out, if you want to retain the formatting for HTML output you could replace with break tags.

$textare_in =~ s/(\r*\n)/<break tag here>/g;

(Couldn't use an actual 'br' tag above because it was taken literally by this BBS script, tried &lt;br&gt; also)

Perl doesn't care how the data looks in the database. If you are worried about retrieving the info, use a different record separator (other than 'newline') to seperate the records in the db.

For example, if you want to store multiple records in a single flat file db and there were multiple entries within each record that could contain newlines, just specify a new record seperator and seperate the entries with something unique (and check that the user did not try to enter either of them).

You could seperate records with, say, ~_~ (tilde, underscore, tilde) and the elements within the record with || (double pipe).

A db could then look like.

This is a record||This is an element||This
is
also||

So is this~_~
This is another record||element

||another element~_~


Then, read it into your script.

my @data;
{ #contain the localization of $/

local $/ = '~_~'; #tell the script what record seperator we are using
while (<DATA>) {
chomp; #removes ~_~
push @data, $_;
}

} #end container

Now @data is made up of the individual records which can be split into the individual elements on the double pipe delimiter.

BigRich



skirrow
stranger

May 13, 2001, 4:24 AM

Post #6 of 6 (1283 views)
Re: Text control for db [In reply to] Can't Post

Thank a lot!!

I changed the type of wrapping and changed
$variable =~ s/\n/ /g;
to
$variable =~ s/(\r*\n)+/ /g;

And it worked brilliantly!

Thanks a lot!

- Neil

--
http://www.cgi-index.com/
http://www.cgiscriptreview.com/

 
 


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

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