在PHP中将字符串转换为UTF-8的最佳方法是什么?

时间:2011-04-28 11:31:03

标签: php mysql encoding utf-8 string-conversion

我想知道使用PHP将字符串转换为UTF-8的最佳方法是。

以下内容在页面上看起来不错,但在MySQL中,某些字符看起来很乱。

  • MYSQL UPGRADE EVENT – New Mazda2 from £124.99 per month. Representative 0% APR over 3 years. Min 50% deposit required. Call xxxxxxxxxxxx for test drive, info or Ts&Cs.
  • ON PAGE UPGRADE EVENT - New Mazda2 from £124.99 per month. Representative 0% APR over 3 years. Min 50% deposit required. Call xxxxxxxxxxxx for test drive, info or Ts&Cs.

我看过iconv,mb_string,html_entity_decode,htmlentities,mb_convert_encoding等。

数据库排序规则是utf8_unicode_ci,表格的字符集是utf8。

2 个答案:

答案 0 :(得分:4)

尝试使用mysql_query("SET NAMES 'utf8'")(或dbh->exec("SET NAMES 'utf8'"))来设置连接的编码。

答案 1 :(得分:1)

你应该设置mysql连接的编码。尝试使用SET NAMES utf8

相关问题