Apostrophes and quotes change to bad characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Apostrophes and quotes change to bad characters

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Apostrophes and quotes change to bad characters

Post 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">
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Apostrophes and quotes change to bad characters

Post 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.
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Apostrophes and quotes change to bad characters

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Apostrophes and quotes change to bad characters

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

:)
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Re: Apostrophes and quotes change to bad characters

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