Welcome to WebmasterWorld Guest from 3.80.6.254

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to maintain format while saving into mysql

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

Junior Member

10+ Year Member

joined:Sept 12, 2009
posts: 137
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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.

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

Junior Member

10+ Year Member

joined:Sept 12, 2009
posts:137
votes: 0


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,

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

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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

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

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


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>

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

Full Member

10+ Year Member

joined:June 1, 2007
posts:201
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


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

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

Junior Member

10+ Year Member

joined:Sept 12, 2009
posts:137
votes: 0


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

Thank you again.