Jump to content

Pulling variables and \n out of a database...


ssmK

Recommended Posts

Hi there, I don't mean to pester you guys with these questions, I usually try to figure this stuff out myself, but this one has me stumped.  I'm making an automated email system for communicating with customers, and have got the mail() function working well.  And, I'd like to store the contents, subjects for different types of email in a MySQL database.

Here's my problem:

[code]mail("Line1\nLine2\nLine3");[/code]
will send this email:

Line1
Line2
Line3

Now, say I store 'Line1\nLine2\nLine3' as a TEXT, and then do something like this (assuming, of course, that I've pulled what I need to into $row...)

[code]mail($row[3]);[/code]
This is what arrives in my mailbox

Line1\nLine2\nLine3

I am also having the same problem with variables.  I can not figure out how to store something like this (stored in MySQL as TEXT) 'Hi, $first_name, thanks...' and then have it be of any use to me without str_replace(). 

I'm guessing I need something of a reverse escape string function, or something that will tell PHP to re-parse the string.  Anyone know how to do this?

Thanks!

Rob

Edit:  I just realized the syntax for mail() all wrong... but hopefully you guys get the idea... thanks again!
Link to comment
Share on other sites

The purpose of this forum is to pester us with questions :)  Then we get a warm fuzzy feeling when we can help!

I'm guessing the problem here is that you are putting a literal string 'Line1\nLine2\nLine3' into the database.. notice the single quotes there.  "\n" is only converted to a linefeed if it's within double quotes.  So, this should work:

[code]$content = "Line1\nLine2\nLine3"; # Double quotes means \n is converted to linefeed
$escaped_content = mysql_real_escape_string($content);
$query = "INSERT INTO table (content) VALUES ('$escaped_content')";[/code]

Try that query and fetch the data back and see if it works.  The key point is that by setting $content using double quotes around the \n characters, the \n are all converted to linefeeds.  After that things should be fine.

Actually you could use str_replace .. str_replace('\n', "\n", $string).  It converts literal '\' and 'n' into the linefeed character "\n".
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.