homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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

 2:46 pm on Sep 25, 2002 (gmt 0)


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.



 4:15 pm on Sep 25, 2002 (gmt 0)

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


 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


... 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.


 10:29 am on Sep 27, 2002 (gmt 0)

Thanks for that Toadhall


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.


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