CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2008
    Posts
    45

    Php - String end of line

    Hello,

    I got a text paragraph that i want to display as it is to do that i need to add a <br> tag to the end of each line the problem is how do i find the end of the line?

    explode is useless here since each line may or may not contain \n or \r at the end so that's not a good way to divide the lines

    i maybe missing something obvious here but unless i can get the position or a way to get to the end of the line there's not much i can do...

    thx

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    All lines (if breaking) contain \r, \n, or \r\n depending on the operating system. You can also try looking into using PHP's file() function. It parses a large string and breaks it into an array delimited by line returns.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    This is a special text it was scanned via scanner then processed via third party program so believe me there are some lines without \n or \r or any other combo as far as i know unless there is a way to see those hidden line breaks etc or a text editor or program that adds them to every line.

    it looks like this btw in the original word document i have say a paragraph with 5 lines if i copy this paragraph to notepad i now have 4 lines or 3 so it rearranges according to the real amount of line brakes and i need to keep the original ratio...

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    Can you attach that document?

    If it doesn't have those hidden characters, then the document does not have any line returns in it at all. Those three ways are how a computer reads line returns.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Ok here is a sample document.
    Attached Files Attached Files
    • File Type: doc 1.doc (26.5 KB, 381 views)

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    Well, there is only one line in that document. That is why it won't find more than one hidden line return character.

    Also, remember that PHP parses plain text a whole lot better than a document with stylized text.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Can you elaborate on your answer a bit
    how can you tell there's only one line and how does parsing helps me here?
    Last edited by Gorbo; February 15th, 2009 at 01:44 AM.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    Just because the document contains multiple lines, that is because it has line wrapping. However, the text itself is not in anyway multiline. Because this is the case, you cannot parse it by lines. You could split it by the number of words, or even by periods. But, the text itself is only one line.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Ok i see that i need to write my own line wrapping function for this, just a few questions left.

    1. Since that was just a sample document and there are some new line characters in the real document what is the easiest way to see them?

    2. What should i add to the end of each line, i.e is there any real difference between \n and \r\n (will the printer actually print differently according to this)?

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    Remember that when working with hidden characters you must use double-quotes and not single-quotes. That's the only way to find/replace/split them.

    The difference between \n, \r, or \r\n is in the operating system which writes the text document. Windows uses \r\n, Mac uses \n, and I cannot remember, but some other base uses just \r, or at least it used to. I do not believe that just \r is common anymore.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Using single quotes only works if the text in the quotes has \n but that's useless since the text itself has hidden \n, i.e i already divided the text by \n so i know there is at least some there
    is there some kind of a print or other command to print hidden characters inside the text and not with the text?
    or did i misunderstood what you said?
    echo "mytext\n"; // hello
    echo 'mytext\n'; // hello\n
    echo 'mytext'; // mytext but it has \n

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    There is no way to print the actual hidden characters, but you can replace the characters with your own custom characters.

    If you want to remove extra line returns from text, you can also use PHP's trim() function. That will remove all whitespace from the beginning and end of the string.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Ok thanks a lot for your help and time i guess this wraps it up...

    p.s
    \r - mac
    \n - linux
    \r\n - windows

  14. #14
    Join Date
    May 2002
    Posts
    10,943

    Re: Php - String end of line

    No. Mac, which has UNIX as it's core, is \n. I'm a Mac user.

    I posted this previously in this thread.
    Windows uses \r\n, Mac uses \n, and I cannot remember, but some other base uses just \r, or at least it used to. I do not believe that just \r is common anymore.
    Last edited by PeejAvery; February 17th, 2009 at 09:07 AM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  15. #15
    Join Date
    Oct 2008
    Posts
    45

    Re: Php - String end of line

    Well if you are a mac user maybe you are right but the last 20 sites i've seen on google said other wise of course maybe there are different OS on mac i don't know...

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured