Hi all,
I've built a simple script that allows a user to create an email which is inserted into a pre-built html template to create a nice looking email.
I have created a form that the user enters the email text, uploads an image etc. However, the problem arises when I try to extract the text from the database and place it into the template. It looks fine when I preview the email with the text inserted, but once this is sent out to the mailing list it doesn't look right. £ (pound) signs are replaced with unrecognisable characters, single quotes have backslashes etc etc.
I've tried things like strip_slashes that work to some extent, but I was wondering if there is a function that can deal with all special characters (such as double/single quotes, pound signs etc) and manipulate them for storage in the database and then 'un' manipulate them when they are extracted from the database and placed in the email template?
Thanks in advance
Storing special characters in database
Moderator: General Moderators
-
mattspriggs28
- Forum Newbie
- Posts: 3
- Joined: Tue Apr 28, 2009 4:36 am
Re: Storing special characters in database
You mean stripslashes, not strip_slashes, right? http://us3.php.net/manual/en/function.stripslashes.php. You shouldn't need more than that, unless you did some binary encoding or something when inserting into the db. Did you use mysql_real_escape_string() when inserting to the DB?
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Storing special characters in database
- On the form and in the email put:
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> - The text editor, which you use to create these files, should be set to UTF-8 and — if it's an option — not be inserting a BOM.
- Ensure your database and — in some cases — individual database tables and fields are also set to UTF-8.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: Storing special characters in database
Regarding the slashes problem: If you can, disable magic_quotes_gpc and do all quoting manually using mysql_real_escape_string() as was suggested by my fellow contributor. If you can't disable magic quotes then Google for a strip slashes solution and wrap it in a conditional block that detects if magic quotes is on so your code will work just the same if magic quotes is on or off, namely, as if it were off.
You only need to quote data that is entering the database, stuff that comes out however, should be HTML escaped with htmlspecialchars(). A lot of people use htmlentities() but they're silly.
You only need to quote data that is entering the database, stuff that comes out however, should be HTML escaped with htmlspecialchars(). A lot of people use htmlentities() but they're silly.