开发者

How to get rid of � using php

I am pulling comments out of the database and have this, �, show up... how do I get rid of it? Is it because of whats in the database or how I'm showing it, I've tried using htmlspecialcha开发者_开发技巧rs but doesn't work.

Please help


The problem lies with Character Encoding. If the character shows up fine in the database, but not on the page. Your page needs to be set to the same character encoding as the database. And vice a versa, if your page that posts to the database character encoding does not match, well it comes out weird.

I generally set my character encoding to UTF-8 for any type of posting fields, such as Comments / Posts. Most MySQL databases default to the latin charset. So you will need to modify that: http://yoonkit.blogspot.com/2006/03/mysql-charset-from-latin1-to-utf8.html

The HTML part can be done with a META tag: <META http-equiv="Content-Type" content="text/html; charset=UTF-8">

or with PHP: header('Content-type: text/html; charset=utf-8'); (must be placed before any output.)

Hopefully that gets the ball rolling for you.


That happens when you have a character that your font doesn't know how to display. It shows up differently in every program, many Windows programs show it as a box, Firefox shows it as a questionmark in a diamond, other programs just use a plain question mark. So you can use a newer display system, install a missing font (like if it's asian characters) or look to see if it's one or two characters that do this and just replace them with something visible.


It might be problem of the way you are storing the information in the database. If the encoding you were using didn't accept accents (à, ñ, î, ç...), then it stores them using weird symbols. Same happens to other language specific symbols. There is probably not a solution for what's already in the database, but you can still save the following inserts by changing the encoding type in mysql.

Cheers


Make sure your database UTF-8 (if it won't solve the problem make sure you specify your char-set while connecting to the database).

You can also encode / decode before entering data to your database.

I would suggest to go with htmlspecialchars() for encoding and htmlspecialchars_decode() for decoding.


Are you passing your charset in mysql_set_charset() with mysql_connect() ???


As others have said, check what your database encoding is. You could try using utf8_encode() or iconv() to convert your character encoding.


Check your code for errors. That's all one can really say considering that you have given us absolutely no details as to what you're doing.

Encoding problems are usually what cause that (are you converting from integers to characters?), so, you fix it by checking if you're converting things properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜