Page 1 of 1

Striping newline's from a string

Posted: Wed Oct 18, 2006 9:42 pm
by w35z0r
I've got a textarea that the user may type in. The idea is to save the entered text as $message into a file, onto one line.

Later I have a program that opens the file with the messages and prints them in a table.

The idea is to have one message per row in this table, However, if the user decides to make a new line in the text area, they can manipulate the file and make multiple messages at once.

This is ugly for my orginazation.

Is there any way that I can take these \n out of the middle of the string? I've tried trim(), but it only works if the vermin are at the begining or end of the string.

Thank you for any help.

Posted: Wed Oct 18, 2006 9:49 pm
by feyd
str_replace() however typically a user wants that formatting to stay intact, so I'd personally store it in an XML container (escaped) if pressed to use a flat file.

Posted: Wed Oct 18, 2006 10:06 pm
by w35z0r
Awesome!

Thanks for pointing me in the right direction. I see what your saying about the user wanting the format intact, so what I did was I had the program that writes the message to replace \n with some obscure set of characters that are not likly to come together (EX: `~`) and then write them.

in the program that reads the file, I have str_replace() read the `~` and turn it into a <br />.

Works well. I did this because I don't know about XML, of course I haven't searched this site for information regarding it yet, but I will.

Thank you for your help!

Posted: Wed Oct 18, 2006 10:25 pm
by feyd
Couldn't you avoid the "middle-man" by going right to <br />?

Posted: Wed Oct 18, 2006 10:27 pm
by John Cartwright
I don't know if this would work, but I do know you need to process newlines with double quotes to use them literally. Perhaps use single quotes when processing your data?

Disclaimer: I've been writting a paper for 5 hours now, I'm tired, and I certainly may be off the ball.

Ahem.. Feyd, correct me will ya?

Posted: Thu Oct 19, 2006 5:35 am
by w35z0r
.... ah.

Yeah... I want a dislcaimer too!

Orginally I was taking out \n and in the html I was putting it back in. But html doesn't treat \n like a newline, so I changed it to <br />.

Hmm, well thanks for pointing that out to me so quickly!

Jcart,

I'm confused, do you mean something like '$message' or ""$message""? If so, how would this help?

Posted: Thu Oct 19, 2006 6:56 am
by feyd
w35z0r wrote:Jcart,

I'm confused, do you mean something like '$message' or ""$message""? If so, how would this help?
In his bleary-eyed state, he was thinking of other (unrelated) issues.

Posted: Thu Oct 19, 2006 10:49 am
by RobertGonzalez
Have you looked at nl2br()? It does what you are doing for you.

Posted: Thu Oct 19, 2006 10:53 am
by feyd
Everah wrote:Have you looked at nl2br()? It does what you are doing for you.
Except it leaves the \r and \n.

Posted: Thu Oct 19, 2006 11:14 am
by RobertGonzalez
I did not know that. How about Matthew Mullenwegs New nl2br function?