\n doesn't work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Bradley-Robbins

    \n doesn't work

    I am writing a program, and I am using \n in double-quoted print statements,
    which should give me a line break. It doesn't work. I'm running Apache 2.0
    and PHP 5, with Mozilla Firefox as a browser. What is wrong?


  • Hans van Kranenburg

    #2
    Re: \n doesn't work

    Michael Bradley-Robbins wrote:[color=blue]
    > I am writing a program, and I am using \n in double-quoted print statements,
    > which should give me a line break. It doesn't work. I'm running Apache 2.0
    > and PHP 5, with Mozilla Firefox as a browser. What is wrong?[/color]

    And... you're not confusing \n <--> <br /> ?

    Hans

    --
    "He who asks a question is a fool for five minutes;
    he who does not ask a question remains a fool forever"

    Comment

    • Janwillem Borleffs

      #3
      Re: \n doesn't work

      Michael Bradley-Robbins wrote:[color=blue]
      > I am writing a program, and I am using \n in double-quoted print
      > statements, which should give me a line break. It doesn't work. I'm
      > running Apache 2.0 and PHP 5, with Mozilla Firefox as a browser. What
      > is wrong?[/color]

      Look at the source (view-source in your browser) or wrap the output in
      <pre></pre> tags.


      JW



      Comment

      • Chris Hope

        #4
        Re: \n doesn't work

        Michael Bradley-Robbins wrote:
        [color=blue]
        > I am writing a program, and I am using \n in double-quoted print
        > statements, which should give me a line break. It doesn't work. I'm
        > running Apache 2.0 and PHP 5, with Mozilla Firefox as a browser. What
        > is wrong?[/color]

        This one needs to go in the FAQ list.

        \n gives you a newline break in the data sent to the browser, so if you
        select the view source option in your browser you'll see the source
        does indeed have a linebreak where you are expecting it. However,
        linebreaks are ignored in HTML. To create a line break in HTML which is
        rendered in the browser, you need the <br> tag.

        --
        Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

        Comment

        • John Bokma

          #5
          Re: \n doesn't work

          Chris Hope wrote:
          [color=blue]
          > Michael Bradley-Robbins wrote:
          >[color=green]
          >> I am writing a program, and I am using \n in double-quoted print
          >> statements, which should give me a line break. It doesn't work. I'm
          >> running Apache 2.0 and PHP 5, with Mozilla Firefox as a browser. What
          >> is wrong?[/color]
          >
          > This one needs to go in the FAQ list.[/color]

          of an HTML group? I am sure it is. :-D.
          [color=blue]
          > \n gives you a newline break in the data sent to the browser, so if you
          > select the view source option in your browser you'll see the source
          > does indeed have a linebreak where you are expecting it. However,
          > linebreaks are ignored in HTML. To create a line break in HTML which is
          > rendered in the browser, you need the <br> tag.[/color]

          Depends on what you want, it could be that you don't *want* the br element.

          --
          John MexIT: http://johnbokma.com/mexit/
          personal page: http://johnbokma.com/
          Experienced programmer available: http://castleamber.com/
          Happy Customers: http://castleamber.com/testimonials.html

          Comment

          • Chris Hope

            #6
            Re: \n doesn't work

            John Bokma wrote:
            [color=blue]
            > Chris Hope wrote:
            >[color=green]
            >> Michael Bradley-Robbins wrote:
            >>[color=darkred]
            >>> I am writing a program, and I am using \n in double-quoted print
            >>> statements, which should give me a line break. It doesn't work. I'm
            >>> running Apache 2.0 and PHP 5, with Mozilla Firefox as a browser.
            >>> What is wrong?[/color]
            >>
            >> This one needs to go in the FAQ list.[/color]
            >
            > of an HTML group? I am sure it is. :-D.[/color]

            Heh - yes it's basic fundamental HTML skills we're talking about here.
            Although I've seen this same question asked here a few times now...
            [color=blue][color=green]
            >> \n gives you a newline break in the data sent to the browser, so if
            >> you select the view source option in your browser you'll see the
            >> source does indeed have a linebreak where you are expecting it.
            >> However, linebreaks are ignored in HTML. To create a line break in
            >> HTML which is rendered in the browser, you need the <br> tag.[/color]
            >
            > Depends on what you want, it could be that you don't *want* the br
            > element.[/color]

            Very true. I was making an assumption based on the same experience other
            people have had when posting here about this problem. Plus it seems
            very unlikely that print "\n" does not output a newline break ;)

            --
            Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

            Comment

            • google_nospam@zaft.com

              #7
              Re: \n doesn't work

              Here's something that may help - especially if you have a bunch of
              \n(s) in a database somewhere that you are generating dynamic pages
              with (that you would like to see the line breaks in).

              Inserts HTML line breaks before all newlines in a string


              This little baby will insert a <BR> before any \n in whatever variable
              or string you pass to it. I use this for pages where users get to type
              info into a field and hit \n\n to create a new paragraph. When they
              submit it to a database, it is raw, but when I pull it out, I pass it
              through nl2br and it adds the <BR>(s) for me.

              Not sure if that's what you are looking for, but it's worth saying just
              in case.

              Shane

              Comment

              • John Bokma

                #8
                Re: \n doesn't work

                Chris Hope wrote:
                [color=blue]
                > people have had when posting here about this problem. Plus it seems
                > very unlikely that print "\n" does not output a newline break ;)[/color]

                Yup, true.

                --
                John MexIT: http://johnbokma.com/mexit/
                personal page: http://johnbokma.com/
                Experienced programmer available: http://castleamber.com/
                Happy Customers: http://castleamber.com/testimonials.html

                Comment

                • Daniel Tryba

                  #9
                  Re: \n doesn't work

                  Chris Hope <blackhole@elec trictoolbox.com > wrote:[color=blue][color=green][color=darkred]
                  >>> This one needs to go in the FAQ list.[/color]
                  >>
                  >> of an HTML group? I am sure it is. :-D.[/color]
                  >
                  > Heh - yes it's basic fundamental HTML skills we're talking about here.
                  > Although I've seen this same question asked here a few times now...[/color]

                  It already is (kind of):

                  msgid: 42289b17$0$2897 5$c5fe704e@news 6.xs4all.nl
                  Subject: [FAQ] HTML is trunctating PHP text

                  Take a look at the source (the first place you should look to see what
                  PHP is actually doing) and you will see it's all there as

                  Comment

                  • Tim Roberts

                    #10
                    Re: \n doesn't work

                    Chris Hope <blackhole@elec trictoolbox.com > wrote:[color=blue]
                    >
                    >Very true. I was making an assumption based on the same experience other
                    >people have had when posting here about this problem. Plus it seems
                    >very unlikely that print "\n" does not output a newline break ;)[/color]

                    It DOES output a newline break. Do a View->Source to prove it.

                    The issue is that newlines in HTML are just plain old white space, and are
                    removed when the page is rendered.

                    To get a line break rendered, you need to output a <br> tag.
                    --
                    - Tim Roberts, timr@probo.com
                    Providenza & Boekelheide, Inc.

                    Comment

                    • Chris Hope

                      #11
                      Re: \n doesn't work

                      Tim Roberts wrote:
                      [color=blue]
                      > Chris Hope <blackhole@elec trictoolbox.com > wrote:[color=green]
                      >>
                      >>Very true. I was making an assumption based on the same experience
                      >>other people have had when posting here about this problem. Plus it
                      >>seems very unlikely that print "\n" does not output a newline break ;)[/color]
                      >
                      > It DOES output a newline break. Do a View->Source to prove it.
                      >
                      > The issue is that newlines in HTML are just plain old white space, and
                      > are removed when the page is rendered.
                      >
                      > To get a line break rendered, you need to output a <br> tag.[/color]

                      I know that, and that's exactly what I posted in my reply to the OP ;)

                      --
                      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

                      Comment

                      • John Bokma

                        #12
                        Re: \n doesn't work

                        Tim Roberts wrote:
                        [color=blue]
                        > To get a line break rendered, you need to output a <br> tag.[/color]

                        Depends :-D. In a <pre> it *does* count, and there are some other places
                        where it counts (sometimes in old browsers at unexpected places).

                        --
                        John MexIT: http://johnbokma.com/mexit/
                        personal page: http://johnbokma.com/
                        Experienced programmer available: http://castleamber.com/
                        Happy Customers: http://castleamber.com/testimonials.html

                        Comment

                        Working...