Forum Moderators: coopster
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.
> 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.