Forum Moderators: coopster

Message Too Old, No Replies

How to maintain format while saving into mysql

         

impact

3:49 am on Dec 2, 2009 (gmt 0)

10+ Year Member



Hello,

Does any one knows how to retrieve or save "email text" into mysql database, exactly in the manner in which it was saved?

I have made a member's section, in my personal website. I am able to save and retrieve mails sent by a member to another member but when i print the email body... the text looses its format.

Well, with all the thing i know about programming, I think to get over this there should be some mysql command or the "Text box" which i use for writing the email body should automatically add "<BR>" behind the scene when the user hit enter button...

Well, this is all i can think.. I will really appreciate if some one explains me how to get over this issue.

Thank you from a novice programmer.

rocknbil

5:27 am on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Print the email body . . . to where? To a browser?

Browsers collapse white space, is this what you mean?

Text should be stored in mysql with the carriage/line returns intact. If you're printing to the browser, you can wrap it in <pre> tags or substitute \n\r for <br> when you read from the database.

impact

2:24 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



Yes, print to a browser. Like you know viewing the email. So how do I substitute \n \r or any other tags, when reading from database?

Thank you,

rocknbil

7:30 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, so it's to a browser, right. Simplest would be to just wrap it in <pre>.

$msg = '<pre>' . $msg . '</pre>';
echo $msg;

That gives you a monospaced font, usually courier, if it's for admin view this is all that's needed. If you want to pretty it up, use PHP methods or preg, which I use simply because I've been using regular expressions for years and am almost getting good at them. :-)

Working example:
<?php
header("content-type:text/html");
$msg ='
Hey man. How\'s it going.
This is just a test.

Did you see that video? Oh
you missed it. It\'s gone now.
Okay, TTL.
';
echo "<p>raw: $msg</p>";

$msg = preg_replace('/[\n\r]+/',"<br>",$msg);

echo "<p>replaced: $msg</p>";
?>
Note that this doesn't address the blank line, you make have to split it on the line breaks in an array to get at that.

Edited to simplify pattern

TheMadScientist

7:57 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want to pretty it up, use PHP methods or preg

What about CSS?
<style type="text/css">
pre { font-family:Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; }
</style>

bkeep

9:30 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



I've been using regular expressions for years and am almost getting good at them. :-)

So what you are saying is we all have something to look forward to in our old age ;)

You could also use nl2br($msg); instead of the regex which should deal with blank lines.
[php.net...]

rocknbil

9:45 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What about CSS?

<facepalm> (at first, then)

The only problem with <pre> is that extremely long lines won't wrap.

So what you are saying is we all have something to look forward to in our old age ;)

Ayyy . . . . who you callin' old?

.
.
.
.
Yeah . . . I am . . . lol . . . the really bad part is, I can actually say I've forgotten more than many people know, and this is not a "brag," and it's not a good thing . . . .

impact

1:56 am on Dec 4, 2009 (gmt 0)

10+ Year Member



Thank you all for the help. I used <pre></pre> with "wordwrap". It now works fine.

Thank you again.