homepage Welcome to WebmasterWorld Guest from 54.167.144.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Preserve Formatting after mySQL Insert from PHP form
or any other formatting really
ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1139 posted 2:46 pm on Sep 25, 2002 (gmt 0)

Hello

Working on an admin section to insert data in a mySQL db. What I would like to be able to do is preserve the formatting when it is inserted and when I get it to spit out the info.

I figure it has something to do with \r but I cannot seem to find much on it.

Are there any other pitfalls that I need to know of like erors that is caused by php and forms. I am doing server side validation to make sure the correct things are inserted.

Cheers and thanks in advance.

 

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1139 posted 4:15 pm on Sep 25, 2002 (gmt 0)

What type of fields are they? Do you mean from a textarea?

toadhall

10+ Year Member



 
Msg#: 1139 posted 5:37 pm on Sep 25, 2002 (gmt 0)

Hi ukgimp

> preserve the formatting

> something to do with \r

If you're dealing with data from a <textarea> and want to preserve carriage returns (\r), and perhaps tabs (\t) used as indents (although tabbing in a text area takes you to the next form element they may be 'cut-and-pasted' to the form), use the nl2br() [php.net] function to change newlines into line breaks and str_replace() [php.net] or ereg_replace() [php.net] to search the string(s) for tabs and replace them with 3 or 4 no-break-spaces.

If you're hard-coding the formatting, and you're running php.exe on a Windows machine, where you would use \n on a *nix machine, use \r\n.

Preserving formatted blocks of code or text can be done with "here document" syntax; any formatting between <<<EOF and EOF will be preserved. The block can be stored in a variable:

$cBlock = <<<EOF

code block

EOF;

... the semi-colon is for ending the variable assignment - not for ending the EOF.
NB - I don't know much more about "here docs" than this.

Before sending your data to the database you might use serialize() [php.net] to maintain data integrity, and then unserialize() [php.net] after retrieving it.

ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1139 posted 10:29 am on Sep 27, 2002 (gmt 0)

Thanks for that Toadhall

the

nl2br($variable) is very easy indeed and works well.

Much better than some of the string replace functions I have encounterd over the last few days.

Cheers

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved