Forum Moderators: open

Message Too Old, No Replies

Extra linefeed in textarea

PHP/Apache/Win

         

ergophobe

8:54 am on Sep 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm reading a file and displaying the contents in a textarea so that it can then be edited and rewritten. The only problem is that when I write the text back to the file, every linefeed gets turned into two linefeeds. How do I get it to write the text back as is?

In other words,


This is one line
And this is another

becomes


This is one line

And this is another

I think this is an HTML problem, not a PHP problem. However, I wonder whether PHP interprets the Windows return [CR][LF] as two new lines and then writes it as [LF][LF] which Windows then turns into [CR][LF][CR][LF]?

Tom

tedster

2:16 pm on Sep 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've run into this in many areas - the interface between *nix and Windows often creates two lines for one. I'm not 100% on this, but your guess sounds right to me.

Nick_W

2:24 pm on Sep 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can only think of reading each line into an array and then checking each element to see if it only contains a \n

Maybe there's an easier way but it's what I'd try ;)

Nick

toadhall

3:24 pm on Sep 27, 2002 (gmt 0)

10+ Year Member



My solution to this was simply to strip_tags() of the returning text and immediately re-process it before echoing in the textarea.

A breakdown of the working script:
The text processing sits in a function at the top of the script.
If the incoming text is new - stripslashes() and echo into the textarea.
else (if it's been edited and is returning) strip_tags(), stripslashes(), and re-process. Then echo to the textarea.

The processing:
stripslashes(),
changes \n to <br>,
<br>[[:space:]]<br> to <p>,
and \t to 9 &nbsp;
(in that order).

ergophobe

6:34 pm on Sep 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, that was stupid (but it *was* 2:00am). I was looking for double newlines \n\n and trying replace with \n but on every save it kept growing. That was because in Windows it was adding a \r which gets converted to a \n at some point. Then when the file is called back up, I guess each \n gets converted to \n\r and so on.

So now I just strip out \r on save and even on the Windows machine it saves in "*nix format". That seems to work fine.


into an array and then checking each element to see if it only contains a \n

I didn't try that, but I'm guessing that it wouldn't work because each line would contain \n\r at a minumum.


My solution to this was simply to strip_tags()

I think that's a solution to a different problem. My stuff was displaying just fine in a browser since the whitespace gets ignored. So there was no issue with <br> and such. It's just that the raw HTML was getting unexpected linefeeds and I think it's just the weirdness of Windows (and I want this to run on both Win and Lin).

Tom

toadhall

12:12 am on Sep 28, 2002 (gmt 0)

10+ Year Member



This solution works on both Win and *nix (just ran it again on both platforms).
I recall the frustration with multiple lines with every return to the textarea, but it's been such a while since I wrote the solution I really can't remember what it was. I tried reverse-engineering and all that came to the top was the strip_tags(). Dunno'. Maybe it was the extra squirt of 3-in-1 in the turbo drive. ;)
Anyway, glad you sorted it out.

ergophobe

12:47 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Maybe it was the extra squirt of 3-in-1 in the turbo drive

For the record, it was WD-40 in the PHP Warp Inverters, but I think the effect is much the same.

Tom