\r and \r\n does not work!

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

    #1

    \r and \r\n does not work!

    Hi im trying to create a csv file from a db and insert a new line for
    each row. All the resources i can find say that the use of \r or the
    use of \r\n are sufficient to generate new lines. However after trying
    this for many hours i have come to the conclusion that this is not the
    case. Can anyone suggest a better method for generating line breaks? or
    tell me what additional code i may require to create line breaks in my
    csv file?

    regards

    Marc

  • Kimmo Laine

    #2
    Re: \r and \r\n does not work!

    "monomaniac 21" <mcyi2mr3@googl email.com> wrote in message
    news:1150105523 .717839.133360@ u72g2000cwu.goo glegroups.com.. .[color=blue]
    > Hi im trying to create a csv file from a db and insert a new line for
    > each row. All the resources i can find say that the use of \r or the
    > use of \r\n are sufficient to generate new lines. However after trying
    > this for many hours i have come to the conclusion that this is not the
    > case. Can anyone suggest a better method for generating line breaks? or
    > tell me what additional code i may require to create line breaks in my
    > csv file?[/color]


    Could you please show your code sample. There might be something else that
    is causing the problem, not \r\n itself. For example there is a difference
    between using single quotes '\r\n' and double quotes "\r\n", only the latter
    form generates a linebreak.

    Another situation where linebreaks are often thought to be missing, is when
    you echo something to html code with linebreaks, which mysteriously
    disappear because any linebreaks in html are interpreted as whitspace and
    therefore reduced to a single space.

    <html><body><pr e>
    <?php echo "here \r\n are \r\n some \r\n linebreaks \r\n for \r\n you"; ?>
    </pre></body></html>

    There is also a difference between different operating systems to what is a
    linebreak. As I recall it, CR is for Mac, LF is for Linux and Windows uses a
    combination of the two, CRLF. It's one of those things I'd fix in the world
    if I had three wishes.

    You might also do this:
    $crlf = "
    "; // now the variable $crlf contains one linebreak.

    // you can insert it into doublequote strings and it works:
    $character = "Fred{$crlf}Fli ntstone";

    HTH

    --
    "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • monomaniac21

      #3
      Re: \r and \r\n does not work!

      Thanks kimmo that's really helpful i never knew of the existence of the
      <pre> tag.

      Kimmo Laine wrote:[color=blue]
      > "monomaniac 21" <mcyi2mr3@googl email.com> wrote in message
      > news:1150105523 .717839.133360@ u72g2000cwu.goo glegroups.com.. .[color=green]
      > > Hi im trying to create a csv file from a db and insert a new line for
      > > each row. All the resources i can find say that the use of \r or the
      > > use of \r\n are sufficient to generate new lines. However after trying
      > > this for many hours i have come to the conclusion that this is not the
      > > case. Can anyone suggest a better method for generating line breaks? or
      > > tell me what additional code i may require to create line breaks in my
      > > csv file?[/color]
      >
      >
      > Could you please show your code sample. There might be something else that
      > is causing the problem, not \r\n itself. For example there is a difference
      > between using single quotes '\r\n' and double quotes "\r\n", only the latter
      > form generates a linebreak.
      >
      > Another situation where linebreaks are often thought to be missing, is when
      > you echo something to html code with linebreaks, which mysteriously
      > disappear because any linebreaks in html are interpreted as whitspace and
      > therefore reduced to a single space.
      >
      > <html><body><pr e>
      > <?php echo "here \r\n are \r\n some \r\n linebreaks \r\n for \r\n you"; ?>
      > </pre></body></html>
      >
      > There is also a difference between different operating systems to what is a
      > linebreak. As I recall it, CR is for Mac, LF is for Linux and Windows uses a
      > combination of the two, CRLF. It's one of those things I'd fix in the world
      > if I had three wishes.
      >
      > You might also do this:
      > $crlf = "
      > "; // now the variable $crlf contains one linebreak.
      >
      > // you can insert it into doublequote strings and it works:
      > $character = "Fred{$crlf}Fli ntstone";
      >
      > HTH
      >
      > --
      > "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
      > spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)[/color]

      Comment

      • Tim Roberts

        #4
        Re: \r and \r\n does not work!

        "monomaniac 21" <mcyi2mr3@googl email.com> wrote:[color=blue]
        >
        >Thanks kimmo that's really helpful i never knew of the existence of the
        ><pre> tag.[/color]

        If you are really generating a pure text file, forget about <pre> and just
        specify Content-Type: text/plain instead of text/html.
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • the DtTvB

          #5
          Re: \r and \r\n does not work!

          For outputting plain text, I always do this:

          <?php
          header('Content-Type: text/plain');
          // Send this header so that browsers won't treat it as HTML
          echo "row1,row2,row3 ,row4\r\n"; // Use double quotes!
          echo "row1,row2,row3 ,row4\r\n";
          echo "row1,row2,row3 ,row4\r\n";
          echo "row1,row2,row3 ,row4\r\n";
          echo "row1,row2,row3 ,row4\r\n";
          echo "row1,row2,row3 ,row4\r\n";
          ?>

          Comment

          Working...