php form textarea saving to file: ^M problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Herrman

    #1

    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,$_PO ST[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)
  • B. Johannessen

    #2
    Re: php form textarea saving to file: ^M problem

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Martin Herrman wrote:[color=blue]
    > 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.[/color]

    How about if("\r\n" == $line) { ... }


    Bob


    -----BEGIN PGP SIGNATURE-----
    Comment: B. Johannessen <bob@db.org> - http://db.org/contact/en/

    iD8DBQFAPKM0ooi sUyMOFlgRAmaLAK CBy79CT/pnKLm217Gg3pBar nwnmwCfaojm
    aXfeZBraCgLzl7C jg5NTInA=
    =Xpn5
    -----END PGP SIGNATURE-----

    Comment

    • André Næss

      #3
      Re: php form textarea saving to file: ^M problem

      Martin Herrman:
      [color=blue]
      > 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,$_PO ST[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.[/color]

      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(Arr ay("\r\n", "\r"), "\n", $input);

      Do this just before you write the data to file.

      André næss

      Comment

      • Chung Leong

        #4
        Re: php form textarea saving to file: ^M problem

        if(!trim($line) ) { ...

        Uzytkownik "Martin Herrman" <m.herrman@stud ent.tue.nl> napisal w wiadomosci
        news:pan.2004.0 2.25.13.19.15.3 82757@student.t ue.nl...[color=blue]
        > 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,$_PO ST[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)[/color]


        Comment

        Working...