【问题标题】:Spanish Characters not Displaying Correctly西班牙语字符显示不正确
【发布时间】:2013-02-01 15:26:32
【问题描述】:

我得到了应该显示西班牙字符的可爱 � 框。 (即:ñ、á 等)。我已经确保我的 meta http-equiv 设置为 utf-8:

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

我还确保页眉也设置为 utf-8:

header('Content-type: text/html; charset=UTF-8');

到目前为止,这是我的代码的开始阶段:

<?php
    setlocale(LC_ALL, 'es_MX');
    $datetime = strtotime($event['datetime']);
    $date = date("M j, Y", $datetime);
    $day = strftime("%A", $datetime);
    $time = date("g:i", $datetime);
?>
    <a href="/<?= $event['request'] ?>.html"><?= $day ?> <?= $time ?></a> 

上面的代码在 where 语句中。我已经读过切换数据库中的排序规则也可能是一个因素,但我已经将它设置为 UTF-8 General ci。另外,该列中唯一的内容是 DateTime 反正它是数字,无论如何都无法整理。

结果:s�bado 8:00

一如既往地非常感谢任何帮助。

【问题讨论】:

  • 您是否强制 php->mysql 数据库连接也为 UTF?如果 php->mysql 连接是别的东西,那么拥有一个 db 和 UTF 格式的 php 输出是没有意义的。 ENTIRE 渲染管道必须是相同的字符集,或加入适当的字符集转换逻辑...如果在任何阶段不匹配,您将得到损坏的字符。
  • php 的 default_charset 是否设置为 utf-8 ?
  • 是的,我也将连接设置为 UTF 8... mysql_set_charset('utf8', $connnection);
  • default_carset... 会在 php ini 文件中吗?
  • 是的 php ini 有 default_charset,如果你无权访问它,请使用 ini_set('default_charset', 'UTF-8');

标签: php mysql date special-characters strftime


【解决方案1】:

PHP/MySQL/UTF-8 中需要考虑的事项

  • 数据库表和文本列应设置为 UTF-8
  • HTML 页面 Content-Type 应设置为 UTF-8

    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

  • PHP 应该发送一个标头通知浏览器期待 UTF-8

    header('Content-Type: text/html; charset=utf-8' );

  • PHP-MySQL 连接应设置为 UTF-8

    mysqli_query("SET CHARACTER_SET_CLIENT='utf8'",$conn);

    mysqli_query("SET CHARACTER_SET_RESULTS='utf8'",$conn);

    mysqli_query("SET CHARACTER_SET_CONNECTION='utf8'",$conn);

  • PHP ini 有 default_charset 设置它应该是 utf-8 如果您无权访问它,请使用ini_set('default_charset', 'utf-8');

【讨论】:

  • 警告:mysqli_query() 期望参数 1 为 mysqli,给定字符串
  • $conn = mysql_connect("host", "user", "pass");
  • 剩下的就是这样... if(!$conn) { die(); } else { mysql_select_db("dbname"); }
  • 你正在使用 mysql_connect 然后尝试 mysql_query("SET CHARACTER_SET_CONNECTION=utf8");这可能会帮助ustrem.org/en/articles/utf8-website-building-en
  • 请使用utf8mb4 而不是utf8。更多信息在这里:medium.com/@adamhooper/…
【解决方案2】:

请检查您的文件编码。它必须是 UTF-8 或没有 BOM 的 UTF-8。

改变你的文件编码。使用 Notepad++(您也可以使用其他可以更改文件编码的编辑器)。在菜单栏中 > 选择 ENCODING > 选择任何 UTF-8 或不带 BOM 的 UTF-8。

UTF-8 和不带 BOM 的 UTF-8 的区别见链接。 What's different between UTF-8 and UTF-8 without BOM?

希望对您有所帮助。 :)

【讨论】:

    【解决方案3】:

    遇到类似问题,我在这里找到了答案。 Not Displaying Spanish Characters

    分辨率是从 UTF-8 更改为 windows-1252。

    (HTML) <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    (PHP) ini_set('default_charset', 'windows-1252');
    

    我的问题是从 CSV 文件中读取西班牙语字符。当我在 Excel 中打开文件时,字符看起来很好。在我的编辑器中,无论预期的字符如何,都会显示奇怪的字符。这种变化似乎符合我的要求。

    【讨论】:

      【解决方案4】:

      我多年来一直遇到这个问题,我找不到任何逻辑,我已经尝试了上述所有解决方案。

      一种解决方案是为所有文本制作 html 代码。 这是我在其他所有方法都失败时使用的功能。

      function span_accent($wordz)
      {
      
      $wordz = str_replace( "Á","&Aacute;",$wordz);
      $wordz = str_replace( "É","&Eacute;",$wordz);
      $wordz = str_replace( "Í","&Iacute;",$wordz);
      $wordz = str_replace( "Ó","&Oacute;",$wordz);
      $wordz = str_replace( "Ú","&Uacute;",$wordz);
      $wordz = str_replace( "Ñ","&Ntilde;",$wordz);
      $wordz = str_replace( "Ü","&Uuml;",$wordz);
      
      $wordz = str_replace( "á","&aacute;",$wordz);
      $wordz = str_replace( "é","&eacute;",$wordz);
      $wordz = str_replace( "í","&iacute;",$wordz);
      $wordz = str_replace( "ó","&oacute;",$wordz);
      $wordz = str_replace( "ú","&uacute;",$wordz);
      $wordz = str_replace( "ñ","&ntilde;",$wordz);
      $wordz = str_replace( "ü","&uuml;",$wordz);
      
      $wordz = str_replace( "¿","&iquest;",$wordz);
      $wordz = str_replace( "¡","&iexcl;",$wordz);
      $wordz = str_replace( "€","&euro;",$wordz);
      $wordz = str_replace( "«","&laquo;",$wordz);
      $wordz = str_replace( "»","&raquo;",$wordz);
      $wordz = str_replace( "‹","&lsaquo;",$wordz);
      $wordz = str_replace( "›","&rsaquo;",$wordz);
      return $wordz;
      }
      

      【讨论】:

      【解决方案5】:

      检查您的代码是否也被编码为 UTF-8 很重要(您可以在许多文本和代码编辑器中看到此属性)。

      因为只有一个符号(黑色方块),可能您使用的是 ISO-8859-1 或 ISO-8859-15。

      【讨论】:

        【解决方案6】:

        你能看到数据库表中的内容是正确的吗,例如用phpmyadmin查看它。如果是,请确保您的 php 文件是 utf8 编码的,请查看您的 ide/editor 配置。

        【讨论】:

          【解决方案7】:

          使用utf8mb4Windows-1252

          ini_set('default_charset', 'utf8mb4');
          

          header('Content-Type: text/html; charset=utf8mb4');
          

          然后使用标签,

          <meta charset="utf8mb4">
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-28
            • 1970-01-01
            • 2023-03-18
            相关资源
            最近更新 更多