以 UTF-8 格式显示法语重音字符时出现问题

发布于 2024-09-07 10:44:39 字数 369 浏览 3 评论 0原文

我正在开发一个用 CakePHP 构建的法语网站。我尝试了多种函数来尝试将文本转换为 UTF-8 并正确显示,但到目前为止还没有成功 - 任何重音字母都显示为带问号的黑色菱形。当我将浏览器中的字符集更改为 ISO-8859-1 时,它们确实显示正确,但我想让 while 站点兼容 UTF-8。我用过:

html_entity_decode($string, ENT_QUOTES, 'UTF-8'); htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); utf8_encode

但没有雪茄。该页面在页眉中设置为 UTF-8

并且 MySQL 数据库也使用 UTF-8。如何让重音字符在 UTF-8 中正确显示?

I'm working on a French language site built in CakePHP. I have tried multiple functions to try and convert the text into UTF-8 and display properly, but have had no success so far - any accented letters are displaying as a black diamond with a question mark. They do display correctly when I change the char set in the browser to ISO-8859-1, but I'd like to make the while site UTF-8 compliant. I have used:

html_entity_decode($string, ENT_QUOTES, 'UTF-8');
htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
utf8_encode

but no cigar. The page is set to UTF-8 in the header

And the MySQL database is using UTF-8 too. How can I get the accented characters to display properly in UTF-8?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山色无中 2024-09-14 10:44:39

检查您的@@character_set_results。默认情况下,MySQL 使用 latin1,而不是 utf8。尝试设置名称utf8mysqli::set_charset

更新:以下是检查正在使用的字符集的方法:

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       | 
| character_set_connection | utf8                       | 
| character_set_database   | utf8                       | 
| character_set_filesystem | binary                     | 
| character_set_results    | utf8                       | 
| character_set_server     | utf8                       | 
| character_set_system     | utf8                       | 
| character_sets_dir       | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+

阅读更多内容 dev.mysql.com

Check your @@character_set_results. By default, MySQL uses latin1, not utf8. Try SET NAMES utf8 or mysqli::set_charset.

Update: here is how you might check the character sets in use:

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       | 
| character_set_connection | utf8                       | 
| character_set_database   | utf8                       | 
| character_set_filesystem | binary                     | 
| character_set_results    | utf8                       | 
| character_set_server     | utf8                       | 
| character_set_system     | utf8                       | 
| character_sets_dir       | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+

Read more on dev.mysql.com.

夏日浅笑〃 2024-09-14 10:44:39

首先:检查您的 php 文件编码!我在 Mac 上工作,我使用 Coda 进行编程,它有一个转换字符集的选项,有时我得到像这样的麻烦并转换为 UTF-8 总能解决它们。我认为Notepad++可以在Windows上做到这一点。 (如果您对 PHP 文件执行此操作,则其中的每个字符串都不需要函数 htmlspecialchars()html_entity_decode 等)

第二:如果您使用 HTML 输出,请检查标题上是否有 。 ..

第三:在您的 MySQL 数据库上执行 @janmoesen 所说的操作。

告诉我一些关于那件事的事情。

First: Check your php files encoding! I work on Mac, I use Coda to program, and it has an option to convert charset's, sometimes i get troubles like this and converting to UTF-8 always fix them. I think that Notepad++ can do that on windows. (If you do this on your PHP files, every strings on them will not need the functions htmlspecialchars(), html_entity_decode, etc )

Second: if you are using HTML Output, check if you have <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> on your header...

Third: Do what @janmoesen said on your MySQL DB.

Tell me something about that.

爱要勇敢去追 2024-09-14 10:44:39

我刚刚解决了一个非常类似的问题,重音字符不显示。我的数据库表是 utf-8 编码的,并且我的 html 标头已正确设置为 utf-8。

我没有为 MySQL 连接指定字符集,在我的例子中我使用的是 PDO。

$this->pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8;", $username, $password);

现在没问题了!

I just fixed a very similar problem with accented characters not displaying. My database table is utf-8 encoded and my html header is correctly set for utf-8.

I hadn't specified a charset for my MySQL connection, in my case I'm using PDO.

$this->pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8;", $username, $password);

No problems now!

尘曦 2024-09-14 10:44:39

如果这对使用 PHP 类的人有帮助。

我的解决方案是将字符编码设置为我的连接。

//connect to MySQL and select DB
public function open_connection() {
    $this->connection = new mysqli( DB_SERVER, DB_USER, DB_PASS, DB_NAME );
    if ( !$this->connection ) {
        die( "Database connection failed: " . mysqli_error() );
    }
    mysqli_set_charset($this->connection,"utf8");
}

In case this helps anyone using PHP Classes.

My solution was to set Character Encoding to my Connection.

//connect to MySQL and select DB
public function open_connection() {
    $this->connection = new mysqli( DB_SERVER, DB_USER, DB_PASS, DB_NAME );
    if ( !$this->connection ) {
        die( "Database connection failed: " . mysqli_error() );
    }
    mysqli_set_charset($this->connection,"utf8");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文