donglu2761 2011-03-31 00:14
浏览 42
已采纳

来自PHP形式的奇怪字符。 字符集?

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 have 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

  • 写回答

1条回答 默认 最新

  • doushi2047 2011-03-31 00:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计