Striping newline's from a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

Striping newline's from a string

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Couldn't you avoid the "middle-man" by going right to <br />?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you looked at nl2br()? It does what you are doing for you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah wrote:Have you looked at nl2br()? It does what you are doing for you.
Except it leaves the \r and \n.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I did not know that. How about Matthew Mullenwegs New nl2br function?
Post Reply