Page 1 of 1
Apostrophes and quotes change to bad characters
Posted: Thu Sep 24, 2009 2:38 pm
by lubber123
I have pages that insert text from a text area into a database. When it does this it changes the apostraphes and quotes to characters such as ’. I have tried using addslashes, $magic_quotes_active, mysql_real_escape_string, stripping all formatting that could be accompanying text by inserting into notepad and then entering - combinations of all the above. I still get these bad characters. So, and I am not a guru at any of this at all, but I am thinking it might have to do with the character set on the server or something like that.
Does anyone have a clue about this and what I could do to get my text inserting properly into the DB?
Thanks.
Re: Apostrophes and quotes change to bad characters
Posted: Thu Sep 24, 2009 2:54 pm
by requinix
The database tables and connection and your HTML pages need UTF-8 encoding.
Code: Select all
CREATE TABLE example (
....
) DEFAULT CHARACTER SET UTF8; # when creating the table
Code: Select all
SET CHARACTER SET UTF8; # for the connection
Code: Select all
// good
header("Content-Type: text/html; charset=utf-8");
Code: Select all
<!-- not as good -->
<meta http-equiv='Content-Type" content="text/html; charset=utf-8">
Re: Apostrophes and quotes change to bad characters
Posted: Thu Sep 24, 2009 2:56 pm
by jackpf
I could swear I just posted in this....
Anyway, what I *meant* to say, was that you shouldn't encode data when inserting it into the database. Encode it on display instead.
Re: Apostrophes and quotes change to bad characters
Posted: Thu Sep 24, 2009 3:18 pm
by lubber123
So what if all your tables have already been created and UTF-8 not added?
Format on display - use string replace? How do you know how to catch all the characters that cold be returned?
Re: Apostrophes and quotes change to bad characters
Posted: Thu Sep 24, 2009 4:56 pm
by jackpf
lubber123 wrote:Format on display - use string replace? How do you know how to catch all the characters that cold be returned?
Personally, I generally run an escape function on insert, like mysql_real_escape_string(), and encode on output, like htmlentities().
If you run htmlentities() with UTF-8 as its encoding, it should catch everything, and display it all properly. That way, you get to keep the original data in the database, it takes up less space, and you don't have the bother of having to save it as UTF-8 which also takes up more space.

Re: Apostrophes and quotes change to bad characters
Posted: Fri Sep 25, 2009 10:07 am
by lubber123
AHHHHHH,
I used the real escape string function and then htmlentities ( VARIABLE , ENT_COMPAT, 'UTF-8') and it seems all is well - thanks a mil for the help.