开发者

Strange characters from PHP form. Character set?

I have a form on my site where users can submit text as part of a product review. The review goes to a MySQL database, where I can review it before approving it so it appears on my site. I received a review today that was filled with strange characters. For example, I think the below was supposed to come out as "fun" but instead it showed up in my MySQL DB as:

“funâ€Â

I'm pretty sure this is a character encoding issue, and I've read a few entries on stackoverflow about such issues, but I'm just not sure how to implement a fix. I'm guessing I need to change the php function I use to do data cleaning from the form, which is below:

function cleanDataForDB($data) {
$data = trim(htmlentities(strip_tags(nl2br($data),'<br><br />')));

if (get_magic_quotes_gpc())
    $data = stripslashes($data);

$data = mysql_real_escape_string($data);

return $data;

}

The html for my site is encoded in UTF-8. I h开发者_开发百科ave this tag at the top of every page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Do I need to use a php encoding function, such as utf8_encode() on data entry and utf8_decode() when I'm displaying in a browser?

Any help is greatly appreciated. Thanks!

Chris


It's also good to make sure that the web server is advertising UTF-8, but that's not the culprit here. I use the Live HTTP Headers extension in Firefox to test. MySQL always defaults to the latin-1 character set and you must explicitly set it other wise with mysql_set_charset(). PHP itself it not very good at multi-byte character sets like UTF-8, but as long as it doesn't need to understand those characters (such as regular expression matching) you are safe. You just need to make sure all input and output to the User (via the meta tag) and to the database are aware of the character encoding.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜