【问题标题】:Some letters are jumbled when outputted to HTML [duplicate]一些字母在输出到 HTML 时混乱 [重复]
【发布时间】:2013-09-19 21:55:09
【问题描述】:

字符 é 在 Chrome 中显示为问号。我正在使用 PHP 从 MySQL 获取内容以输出到 HTML。我尝试使用mb_detect_encoding() 来确定内容的编码,有些是ASCII,有些是UTF-8。为什么它们有不同的编码?是 MySQL 让它们与众不同还是 PHP 这样做了?

包含é 的内容被编码为UTF-8。但如果我在内容上使用utf8_encode(),则字符显示正确。如果已经是 UTF-8,为什么使用utf8_encode() 可以正确显示?

【问题讨论】:

  • é 是 ASCII 码 233 码如果你转换没有区别。
  • 你也在填充数据库吗?

标签: php html mysql encoding utf-8


【解决方案1】:

您必须将与您的数据库的连接定义为 utf8:

// Setup your connection
$connection = mysql_connect('localhost', 'user', 'pw');
mysql_select_db('yourdb', $connection);
mysql_query("SET NAMES 'utf8'", $connection);

// now you get utf-8 encoded stuff
$query = sprintf('SELECT name FROM place where id = 1');
$result = mysql_query($query, $connection);
$result = mysql_fetch_assoc($result);

【讨论】:

    【解决方案2】:

    您可以使用htmlentities() PHP 函数对字符串进行转义,以便将字符更改为各自的HTML 实体,例如è 更改为è,无论您是否指定编码类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多