Click to See Complete Forum and Search --> : Php - String end of line
Gorbo
February 12th, 2009, 05:58 PM
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
PeejAvery
February 12th, 2009, 07:04 PM
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() (http://us2.php.net/manual/en/function.file.php) function. It parses a large string and breaks it into an array delimited by line returns.
Gorbo
February 12th, 2009, 11:27 PM
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...
PeejAvery
February 13th, 2009, 07:40 AM
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.
Gorbo
February 13th, 2009, 06:21 PM
Ok here is a sample document.
PeejAvery
February 14th, 2009, 09:40 AM
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.
Gorbo
February 14th, 2009, 03:51 PM
Can you elaborate on your answer a bit
how can you tell there's only one line and how does parsing helps me here?
PeejAvery
February 15th, 2009, 05:30 PM
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.
Gorbo
February 16th, 2009, 09:10 AM
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)?
PeejAvery
February 16th, 2009, 09:17 AM
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.
Gorbo
February 16th, 2009, 05:03 PM
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
PeejAvery
February 16th, 2009, 05:34 PM
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.
Gorbo
February 16th, 2009, 10:06 PM
Ok thanks a lot for your help and time i guess this wraps it up... :)
p.s
\r - mac
\n - linux
\r\n - windows
PeejAvery
February 17th, 2009, 07:00 AM
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.
Gorbo
February 17th, 2009, 10:41 AM
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...
PeejAvery
February 17th, 2009, 11:20 AM
What sites? I'd be interested to see those.
Mac has been UNIX based, which includes the \n, since the introduction of Mac OS X in 2001.
Gorbo
February 17th, 2009, 04:01 PM
Grr i don't remember most of them but you can check wikipedia for instance
http://en.wikipedia.org/wiki/Newline
* LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
* CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
* CR: Commodore machines, Apple II family, Mac OS up to version 9 and OS-9
PeejAvery
February 17th, 2009, 07:50 PM
Ah...yes, Mac OS 9 is (was) \r. With the major update in 2001 to OS X, the whole platform was rebuilt.
Thanks for clearing that up! :wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.