473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

php form textarea saving to file: ^M problem

Dear programmers,

I have a form:

<form action=".." method="post">
<textarea name="text1"></textarea>
</form>

And a php file that saves this variable to a file:

$fp = fopen("file.txt",a);
fwrite($fp,$_POST[text1]);
fclose($fp);

When I fill the textarea with the following text:

1,2,
3,

4.

In the file, the following appears (reading the file with Midnight
Commander on Linux):

1,2,^M
3,^M
^M
4.

In a new script, I want to parse this file. Every empty line should be
detected. How should I do that? if ($line == "\n") { .. } doesn't work,
because the line actualy contains ^M\n. When saving the contents of the
textarea to the file, I don't know how to strip the ^M characters.

Any ideas?

much thanks in advance!

Martin
(The Netherlands)
Jul 17 '05 #1
3 2793
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Martin Herrman wrote:
In a new script, I want to parse this file. Every empty line should be
detected. How should I do that? if ($line == "\n") { .. } doesn't work,
because the line actualy contains ^M\n. When saving the contents of the
textarea to the file, I don't know how to strip the ^M characters.


How about if("\r\n" == $line) { ... }
Bob
-----BEGIN PGP SIGNATURE-----
Comment: B. Johannessen <bo*@db.org> - http://db.org/contact/en/

iD8DBQFAPKM0ooisUyMOFlgRAmaLAKCBy79CT/pnKLm217Gg3pBarnwnmwCfaojm
aXfeZBraCgLzl7Cjg5NTInA=
=Xpn5
-----END PGP SIGNATURE-----
Jul 17 '05 #2
Martin Herrman:
Dear programmers,

I have a form:

<form action=".." method="post">
<textarea name="text1"></textarea>
</form>

And a php file that saves this variable to a file:

$fp = fopen("file.txt",a);
fwrite($fp,$_POST[text1]);
fclose($fp);

When I fill the textarea with the following text:

1,2,
3,

4.

In the file, the following appears (reading the file with Midnight
Commander on Linux):

1,2,^M
3,^M
^M
4.

In a new script, I want to parse this file. Every empty line should be
detected. How should I do that? if ($line == "\n") { .. } doesn't work,
because the line actualy contains ^M\n. When saving the contents of the
textarea to the file, I don't know how to strip the ^M characters.


In case you didn't know, unix uses \n for newlines, Mac uses \r (older macs
anyway, OSX is based on FreeBSD and uses \n) and Windows uses \r\n.

I'd suggest making sure that newlines are always represented the same,
regardless of what system the data was typed in on. The simplest way to
achieve this is probably to do a str_replace(). You want to first replace
any \r\n with \n, then any \r with \n. Thus:

str_replace(Array("\r\n", "\r"), "\n", $input);

Do this just before you write the data to file.

André nęss
Jul 17 '05 #3
if(!trim($line)) { ...

Uzytkownik "Martin Herrman" <m.*******@student.tue.nl> napisal w wiadomosci
news:pa****************************@student.tue.nl ...
Dear programmers,

I have a form:

<form action=".." method="post">
<textarea name="text1"></textarea>
</form>

And a php file that saves this variable to a file:

$fp = fopen("file.txt",a);
fwrite($fp,$_POST[text1]);
fclose($fp);

When I fill the textarea with the following text:

1,2,
3,

4.

In the file, the following appears (reading the file with Midnight
Commander on Linux):

1,2,^M
3,^M
^M
4.

In a new script, I want to parse this file. Every empty line should be
detected. How should I do that? if ($line == "\n") { .. } doesn't work,
because the line actualy contains ^M\n. When saving the contents of the
textarea to the file, I don't know how to strip the ^M characters.

Any ideas?

much thanks in advance!

Martin
(The Netherlands)

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asad | last post by:
I have a form on a page that has several textareas, and textboxes inside a table (so the table containing the textboxes is also inside the FORM tag). I want to replace the textareas with simple...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
2
by: alwaysintune | last post by:
I'm using the McFedries email form, and I can't seem to get an upload form to work. Instead of it saving to my server, I want it to send the information and the picture to my email. Here is the...
5
by: Schraalhans Keukenmeester | last post by:
I want to build a very basic online text editor, to allow customers to modify their own sites' html content using a simple form with textarea element. I read the HTML file like so: <?PHP ...
1
by: fugaki | last post by:
Hi everyone I'm learning asp, and i downloaded this script to teach me how to post form data from a webpage to an access database. I put it on the server so i could make sure that it worked, and...
4
by: rn5a | last post by:
I am storing the physical path of images, along with 3 more columns, in a MS-Access DB table. I want to provide users the option to change this physical image path (as well as the records in the...
3
by: mirianCalin | last post by:
the code saves the category, image title, image, and feature.. but the problem is that the "feature" is not saved, but the others were saved.. this is the data types of my table category = text...
3
by: saytri | last post by:
I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is...
4
by: bleachie | last post by:
Hi, My e-mail form seems to work fine in IE7 but doesn't work in FireFox2.0 - it just goes to the index.php instead of echoing the completed message. Hope someone can help me out. Form.html...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.