SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Line breaks in plain text ? ?

    Anyone know how to code a line break in a plain text file (if it's any help, the file extn is .txt).

  2. #2
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Whitespace, including line breaks, are preserved in text files, so if you're hardcoding it, simply add a regular line break.

    If you have to generate the file, you can add a new line with \n.

    (Note that, technically, you should use the 0x0d 0x0a character sequence in single-byte character sets, and 0x000d 0x000a sequence in double-byte character sets, but I have never encounted any problems with the single \n).
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  3. #3
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    (Note that, technically, you should use the 0x0d 0x0a character sequence in single-byte character sets, and 0x000d 0x000a sequence in double-byte character sets, but I have never encounted any problems with the single \n).
    Should according to whom?
    Simon Pieters

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Note that 0x0a and 0x000a are exactly the same number. It doesn't matter whether you specify the number as 10 or 0010 or 00000000010 it is still TEN. Adding extra zeros to the front of a number no matter what the number base doesn't change it.

    \n is just a convenient shortcut that has been assigned so that people don't have to remember that a new line character (which is what the n represents) is the tenth character in the ASCII and Unicode character sets.

    All the 0x on the front of the number means is that it is base 16 instead of base 10.

    0 1 2 3 4 5 6 7 8 9 a b c d e f 10
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Note that 0x0a and 0x000a are exactly the same number.
    Not in character encoding it isn't... Well, it is and it isn't... You send 0x000A to a 8 bit character set, you'll get null followed by a carriage return, instead of just a null -- on little endian systems. Big endian it's the other way around. Null could be bad, as most C strings are null terminated. Could result in the entire rest of the file not even displaying if you sent the word-length version to the wrong encoding.

    Oh, and it would help if we were to say what the characters ARE instead of just spouting off their numbers. Carriage return (0x0D - /r) and Line feed (0x0A - /n)... which stem back to the typewriter, teletype and serial terminal days.

    TECHNICALLY by what the characters mean in ASCII, line feed should ONLY move the cursor down without setting it to beginning of line, which is why DEC PDP, CP/M, TOS, and anything DOS based requires both /r/n. CR+LF.

    Apple (from the II right up to MacOS-9), old Trash-80's, use carriage return only... This was mostly done originally to save that one byte.

    Naturally like everything else *nix, they use the one that makes the least sense, linefeed only. This actually makes *nix incompatible with a lot of older terminals unless you tell it what the terminal is and have software translate it.

    Thankfully across most systems the unrecognized character is usually ignored -- and at worst you just get double-spacing... so CR+LF works on 99% of the world. Unless you're messing around with old Sinclairs it's unlikely to be an issue - though it is why most editors still let you chose how newlines are handled.

    quote for truthiness:
    http://en.wikipedia.org/wiki/Newline

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,872
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    Not in character encoding it isn't... Well, it is and it isn't... You send 0x000A to a 8 bit character set, you'll get null followed by a carriage return, instead of just a null -- on little endian systems. Big endian it's the other way around.
    Of course when you need numbers to be a specific length then of course you need to pad them with zeros. So it should only be if you pad with zeros unnecessarily that you would get a problem as then it can be misidentified as being multiple characters.

    Not including the leading zeros should mean that you don't need to concern yourself with little-endian versus big-endian as the system should add the zeros in the right place provided that the program is intelligent enough to know how numbers work.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Not including the leading zeros should mean that you don't need to concern yourself with little-endian versus big-endian as the system should add the zeros in the right place provided that the program is intelligent enough to know how numbers work.
    An assumption I wouldn't make about any compiler or interpreter -- but I'm an assembly language coder at heart; where all things have to be precise and specific...

    Assuming the compiler, interpreter or assembler even lets you do it in the first place, which is where I think a lot of the real problem lies in things like PHP and HTML -- WAY too permissive and allows you to make too many assumptions; net result being sloppy coding and people making poor decisions because they never learn how it works under the hood.

  8. #8
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the ideas on this - it's appreciated. I tried \r\n but they actually came out in the emails exactly like that?

    If it helps, it's for auto emails (activation) from a phpbb forum.

  9. #9
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Simon
    RFC 2616, section 3.7.1 and RFC 959, section 3.1.1.1. Probably also others. But, as I also mentioned, \n will to the trick. It's a bit like how browsers should display a lot of >'s around the interwebs, because of faux-XHTML and NET-enabled HTML.

    Stephen
    I know, but in the (probably very unlikely) event that the original poster decides to code the text files in a hex editor in Unicode, and used 0x0a 0x0d in stead of 0x000a 0x000d, it would give unexpected results.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  10. #10
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Can we see the code you are inserting it into?
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  11. #11
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    Can we see the code you are inserting it into?
    It's all just plain text, no code.

  12. #12
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    But then, with single quotes, those line breaks could just as easily by simply adding the line break to the text.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  13. #13
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    As originally stated, if it's plain text, you can simply add the line breaks using [Return].
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  14. #14
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by C. Ankerstjerne View Post
    As originally stated, if it's plain text, you can simply add the line breaks using [Return].
    Yep, and that doesn't work!

  15. #15
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dez View Post
    Yep, and that doesn't work!
    Silly question, but what are you doing with said plaintext file? Is some editor you are trying to use stripping them? Are they not showing up when you echo....

    Being you asked in the HTML forum, are you outputting it in HTML? If so, you need to put it inside PRE tags for the carriage returns to show... That or replace all \n with <br />... or if it's inside some other tag, add white-space: pre; in the CSS.

    HTML is whitespace neutral -- so all carriage returns and spaces between words are collapsed into single spaces -- that's the only situation I can think of where what you describe would happen.

    Though that's a wild guess. Again, without seeing the file and what you are trying to do with the file -- we're all just guessing. Educated guesses, but still just guessing.

  16. #16
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    i am also facing this problem of inserting LF CR into a text editor like geany (linux

    I am also facing this problem of inserting a linefeed LF CR in geany / gedit linux. In geany, when I open a file I can see the characters LF for line feeds. But which shortcut key we have to use for inserting the visible letters LF ?

  17. #17
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Purpose of above: Use LF or LFCR as delimiter for exploding a string into small pieces using PHP explode function.

  18. #18
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    If you are using double quotes (") and the file is being parsed as PHP and the escape characters (\r\n) are still showing, then it has to do with how the scrips handles the files. My guess (based on your saying that it's phpBB) would be that the code is not being parsed as PHP at all, but rather as HTML. Does it work if you use <br>?
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •