UTF-8 字符无法正确显示

发布于 2024-11-03 02:54:10 字数 356 浏览 6 评论 0原文

这是我的 PHP 代码:

<?php
$result = '';
$str = 'Тугайный соловей';
for ($y=0; $y < strlen($str); $y++) {
    $tmp = mb_substr($str, $y, 1);
    $result = $result . $tmp;
}
echo 'result = ' . $result;

输出是:

Тугайный Ñоловей

我能做什么?我必须将 $result 放入 MySQL 数据库中。

This is my PHP code:

<?php
$result = '';
$str = 'Тугайный соловей';
for ($y=0; $y < strlen($str); $y++) {
    $tmp = mb_substr($str, $y, 1);
    $result = $result . $tmp;
}
echo 'result = ' . $result;

The output is:

Тугайный Ñоловей

What can I do? I have to put $result into a MySQL database.

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

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

发布评论

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

评论(7

老旧海报 2024-11-10 02:54:10

你的文件的编码是什么?也应该是UTF8。你的http服务器的默认字符集是什么?它也应该是UTF-8。

编码仅在以下情况下有效:

  • 文件编码正确
  • 服务器告诉所传送文件的编码是什么。

使用数据库时,您还必须为数据库字段以及 MySQL 客户端与服务器通信的方式设置正确的编码(请参阅 mysql_set_charset() )。仅字段是不够的,因为您的 MySQL 客户端(在本例中为 PHP)可能默认设置为 ISO 并重新解释数据。所以你最终得到 UTF8 DB -> ISO客户端->注入到 UTF8 PHP 脚本中。难怪最后为什么会搞砸:-)

如何使用正确的字符集提供文件?

header('Content-type: text/html; charset=utf-8') 是一种解决方案。

包含 AddDefaultCharset UTF-8 的 htaccess 文件是另一种

HTML 元内容 - type 也可能有效,但最好使用 HTTP 标头发送此信息。

PS:您还必须使用 mb_strlen() 因为 UTF8 字符串上的 strlen() 报告的长度可能会超过实际长度。

What's the encoding of your file? It should be UTF8 too. What's the default charset of your http server? It should be UTF-8 as well.

Encoding only works if:

  • the file is encoded correctly
  • the server tells what's the encoding of the delivered file.

When working with databases, you also have to set the right encoding for your DB fields and the way the MySQL client communicates with the server (see mysql_set_charset()). Fields only are not enough because your MySQL client (in this case, PHP) could be set to ISO by default and reinterprets the data. So you end up with UTF8 DB -> ISO client -> injected into UTF8 PHP script. No wonder why it's messed up at the end :-)

How to serve the file with the right charset?

header('Content-type: text/html; charset=utf-8') is one solution

.htaccess file containing AddDefaultCharset UTF-8 is another one

HTML meta content-type might work too but it's always better to send this information using HTTP headers.

PS: you also have to use mb_strlen() because strlen() on UTF8 strings will probably report more than the real length.

夏见 2024-11-10 02:54:10

如果您要发送混合数据并且不想使用 php 标头指定 utf-8,则可以将此 html 添加到您的页面:

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

If you're going to send a mix of data and don't want to specify utf-8 using a php header, you can add this html to your page:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
不甘平庸 2024-11-10 02:54:10

我想,您的代码采用 windows-1251 编码,因为它是俄语:)
将字符串转换为 utf-8:

$str = iconv('windows-1251', 'utf-8', $str);

I suppose, your code is in windows-1251 encoding since it is Russian :)
convert your string to utf-8:

$str = iconv('windows-1251', 'utf-8', $str);
盗梦空间 2024-11-10 02:54:10

如果你的数据库是UTF-8的话,mysql就可以了。

对于您的回声,如果您在网站中执行此操作,请将其放在首页中:

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

If your database is UTF-8, it's ok for mysql.

For your echo, if you do it in a web site, put this in the top page:

header('Content-Type: text/html; charset=UTF-8');
那小子欠揍 2024-11-10 02:54:10

只需在与服务器连接后的开头添加此行:

mysqli_set_charset($conn,"utf8");

Just add this line at the beginning, after the connection with server:

mysqli_set_charset($conn,"utf8");
晨光如昨 2024-11-10 02:54:10

试试这个:

    header('Content-Type: text/html; charset=UTF-8');
    header("Content-type: application/octetstream");
    header("Pragma: no-cache");
    header("Expires: 0");
    //print "$name_field\n$data";

    // با این کد درست شد
    print chr(255) . chr(254) . mb_convert_encoding("$name_field\n$data", 'UTF-16LE', 'UTF-8');

try this:

    header('Content-Type: text/html; charset=UTF-8');
    header("Content-type: application/octetstream");
    header("Pragma: no-cache");
    header("Expires: 0");
    //print "$name_field\n$data";

    // با این کد درست شد
    print chr(255) . chr(254) . mb_convert_encoding("$name_field\n$data", 'UTF-16LE', 'UTF-8');
浪荡不羁 2024-11-10 02:54:10

如果你只是使用 PHP echo 而不使用 HTML headers 等,这对我来说非常有用。

$connect = mysqli_connect($host_name, $user_name, $password, $database);
mysqli_set_charset($connect,"utf8");

if you are just using PHP echo with no HTML headers etc., this worked great for me.

$connect = mysqli_connect($host_name, $user_name, $password, $database);
mysqli_set_charset($connect,"utf8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文