Page 1 of 1

problem with line endings

Posted: Wed Nov 12, 2003 6:38 am
by siteresources
hi,

I seem to be having trouble with line endings. Here is what the script is suppose to do:

1. it is connected to an ftp server, and it gets the selected file from there, and puts it into a directory on the server which the script is on.
2. then, it is suppose to read the file, through fopen, and fread, and those file functions
3. then it is to return the string, and the function highlight_string($text) is used.

Now, my problem, however, is that, when it comes back and it is run through that highlight_string function, it goes as one big line, of course it wraps to other lines, but the only thing I can figure out is that there is something wrong with the line endings.

if I go into that file, cut all the code out of it, and paste it back in, it works fine.

is there some type of problem with line endings going into windows from some other system? I have tried a few things. I tried using file() and readfile, and I always get the same thing.

would it work if I cut off the line endings with trim, used file(), and added the line ending manually? for example:

Code: Select all

$filename = "C:/www/temp";
$result = file($filename);

$text = ""
foreach ($result as $line) {
$text .= trim($line)."\n";
}
or something similar to that? I'm going to try it out anyway to see if it might work. but any help would be appreciated.

Brandon

Posted: Wed Nov 12, 2003 6:47 am
by JayBird
on a windows system i think you should have \r\n for the line ending

Mark

Posted: Wed Nov 12, 2003 6:54 am
by siteresources
hi,

Thanks, yeah that's what I think to, but how do I make the fil ehave those line endings? the method I said I'd try in my post didn't work. so any idea of how I could do this?

Brandon
Bech100 wrote:on a windows system i think you should have \r\n for the line ending

Mark

Posted: Wed Nov 12, 2003 6:59 am
by twigletmac
Does:

Code: Select all

$fcontents = file($filename);
$text = implode('', $fcontents);
work, or if you are using PHP 4.3 up:

Code: Select all

$text = file_get_contents($filename);
Mac

Posted: Wed Nov 12, 2003 7:07 am
by siteresources
hi,

That produces the same results as the other methods.

example: if the first three lines of my file looked like:

Code: Select all

<?php
/* script name */
$variable = $_POST["variable"];
when viewing it, it would look like:

Code: Select all

<?php /* script name */ $variable = $_POST["variable"]

Brandon
twigletmac wrote:Does:

Code: Select all

$fcontents = file($filename);
$text = implode('', $fcontents);
work, or if you are using PHP 4.3 up:

Code: Select all

$text = file_get_contents($filename);
Mac

Posted: Wed Nov 12, 2003 7:27 am
by twigletmac
Ok, that's kinda strange as I've tested the code and:

Code: Select all

$fcontents = file($filename);
$text = implode('', $fcontents);
echo highlight_string($text);
works as expected for me. What do you get if you do:

Code: Select all

echo nl2br($text);
?

Mac

Posted: Wed Nov 12, 2003 7:52 am
by siteresources
hi,

Yes it is strange, but remember this is coming onto the server by being downloaded through ftp_get, so it might be corrupted somehow?

nl2br works fine! but, of course, that prevents me from using highlight_sstring, doesn't it?

it should be noted that when viewing, it does look fine when you look at the source code. you see, without the highlight_string, and with htmlentities

Code: Select all

&lt;?php

/* wahtever comments */

etc etc
but, when using highlight_string, it sees that whole text as one line.

but, like I said, nl2br does work.

Brandon
twigletmac wrote:Ok, that's kinda strange as I've tested the code and:

Code: Select all

$fcontents = file($filename);
$text = implode('', $fcontents);
echo highlight_string($text);
works as expected for me. What do you get if you do:

Code: Select all

echo nl2br($text);
?

Mac

Posted: Wed Nov 12, 2003 7:58 am
by siteresources
ha! you wouldn't imagine what did work. this was perfect, and maybe you can think of a better way around this.

Code: Select all

nl2br($text);
$text = str_replace("<br />", "\n", $text);
highlight_string($text);
worked perfectly! I guess that corrects the end line error? is there any better way? because for a person to see that in one's code is almost laughable, since that...well would seem like it doesn't get anywhere, to put the br's in and take them out immediately after...

Brandon

Posted: Wed Nov 12, 2003 8:18 am
by twigletmac
Out of interest, was the original file created on a different operating system? Windows uses \r\n for line breaks, Unix/Linux uses \n and Mac OS \r, which can cause issues.

I agree that it seems strange to put the breaks in and then take them out again but if there's no other solution...

Mac

Posted: Wed Nov 12, 2003 8:21 am
by siteresources
actually, no, I was actually entering my own computer's ftp server. but the only thing I can come up with is the server messes up the files somehow. who knows.

Brandon
twigletmac wrote:Out of interest, was the original file created on a different operating system? Windows uses \r\n for line breaks, Unix/Linux uses \n and Mac OS \r, which can cause issues.

I agree that it seems strange to put the breaks in and then take them out again but if there's no other solution...

Mac

Posted: Wed Nov 12, 2003 8:31 am
by siteresources
one more note I should make.

when doing this, I found that I had to use the following:

Code: Select all

$text = htmlentities($text);
$text = nl2br($text);
$text = str_replace(array("<br />", "<br>"), "\n", $text);
$text = html_entity_decode($text);
highlight_string($text);
had to add the extra, because if I don't, then the br's which are suppose to be in the file, will be converted to \n's instead. so have to convert all html entities first, then add the br's, then remove them and add the correct newlines, and then decode again. just wanted to add that

Brandon