php Codeigniter 和字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8156900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:04:17  来源:igfitidea点击:

Codeigniter and charsets

phpcodeignitercharacter-encoding

提问by Roy

I'm using Codeigniter not for so long but I've some charset problems.. I'm asking around at the CI Forum, but I want to go further, still no global solution: http://codeigniter.com/forums/viewthread/204409/

我使用 Codeigniter 的时间不长,但我有一些字符集问题.. 我在 CI 论坛上询问,但我想更进一步,仍然没有全球解决方案:http: //codeigniter.com/forums/视图线程/204409/

The problem was a database error 1064. I've got a solution, use iconv! Works fine, but I think it's not necessary. I'm searching a lot on the internet for charset's etc but I'm using CI now, how about charsets and CI...

问题是数据库错误1064。我有一个解决方案,使用iconv!工作正常,但我认为没有必要。我在互联网上搜索了很多字符集等,但我现在正在使用 CI,字符集和 CI 怎么样...

So I've a lot of question about it, I hope someone can make it clear for me:

所以我有很多关于它的问题,我希望有人能帮我说清楚:

What's the best way to set the charset global? And what to set?

设置字符集全局的最佳方法是什么?以及要设置什么?

  • In the head

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

  • In config/config.php

    $config['charset'] = 'UTF-8';

  • In config/database.php

    $db['default']['char_set'] = 'utf8';

    $db['default']['dbcollat'] = 'utf8_general_ci';

  • In .htaccess, my rewrite rules and

    php_value magic_quotes_gpc Off

    AddDefaultCharset UTF-8

  • Also need send a header? Where to place? Something like?

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

  • In my editor (Notepad++) save files as UTF-8? Or UTF-8 (without BOM)? Or is ANSI good (this is what I'm using now)?

  • Use utf8_unicode_ci or utf8_general_ci for the MySQL database? And why?

  • How about reading RSS feeds, how to handle multiple charsets? Where I'm working on I've two feeds, one with UTF-8 encoding and the other with ISO-8859-1. This will be stored in the database and will be compared sometimes to see if there are new items. It fails on special chars.

  • 在头上

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

  • 在 config/config.php

    $config['charset'] = 'UTF-8';

  • 在 config/database.php

    $db['default']['char_set'] = 'utf8';

    $db['default']['dbcollat'] = 'utf8_general_ci';

  • 在 .htaccess 中,我的重写规则和

    php_value magic_quotes_gpc Off

    AddDefaultCharset UTF-8

  • 还需要发送标头吗?放在哪里?就像是?

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

  • 在我的编辑器 (Notepad++) 中将文件保存为 UTF-8?还是 UTF-8(没有 BOM)?还是 ANSI 好(这是我现在使用的)?

  • MySQL 数据库使用 utf8_unicode_ci 或 utf8_general_ci 吗?为什么?

  • 如何阅读 RSS 提要,如何处理多个字符集?我工作的地方有两个提要,一个使用 UTF-8 编码,另一个使用 ISO-8859-1。这将存储在数据库中,有时会进行比较以查看是否有新项目。它在特殊字符上失败。

I'm working with: - CI 2.0.3 - PHP 5.2.17 - MySQL 5.1.58

我正在使用: - CI 2.0.3 - PHP 5.2.17 - MySQL 5.1.58

More information added:

添加了更多信息:

Model:

模型:

function update_favorite($data) 
{
 $this->db->where('id', $data['id']);
 $this->db->where('user_id', $data['user_id']);
 $this->db->update('favorites', $data);
 return;
}

Controller:

控制器:

$this->favorites_model->update_favorite(array(
 'id' => $id, 
 'rss_last' => $rss_last,
 'user_id' => $this->session->userdata('user_id')
)); 

When $rss_last is a “normal” value like: “test” (without quotes) it works fine. When it's a value with more length like (in Dutch): F-Secure vindt malware met certificaat van Maleisische overheid

当 $rss_last 是“正常”值时,例如:“test”(不带引号),它工作正常。当它是一个长度更长的值时(荷兰语):F-Secure vindt 恶意软件满足 certificaat van Maleische overheid

I get this error:

我收到此错误:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘vindt malware met certificaat van Maleisische overheid, user_id= ‘1' WHERE `i' at line 1

UPDATE favoritesSET id= ‘15', rss_last= F-Secure vindt malware met certificaat van Maleisische overheid, user_id= ‘1' WHERE id= ‘15' AND user_id= ‘1'

Filename: /home/.../domains/....nl/public_html/new/models/favorites_model.php

Line Number: 35

错误编号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在“vindt 恶意软件满足 certificaat van Maleisische overheid, user_id= '1' WHERE ‘i' at line 1附近使用正确的语法

UPDATE favoritesSET id= '15', rss_last= F-Secure vindt 恶意软件符合 certificaat van Maleisische overheid, user_id= '1' WHERE id= '15' AND user_id= '1'

文件名:/home/.../domains/....nl/public_html/new/models/favorites_model.php

行号:35

Someone at the CI forum told me to use this:

CI论坛上有人告诉我用这个:

'rss_last' => iconv("UTF-8", "UTF-8//TRANSLIT", $rss_last) 

This works fine, but I think this is not necessary..

这工作正常,但我认为这没有必要..

The value $rss_last came out a RSS feed, as told before, sometimes a UTF-8 and other times a ISO-8859-1 encoding:

如前所述,$rss_last 值来自 RSS 提要,有时是 UTF-8,有时是 ISO-8859-1 编码:

$rss = file_get_contents('http://www.website.com/rss.xml');
$feed = new SimpleXmlElement($rss);
$rss_last = $feed->channel->item[0]->title;

It looks like this last part is the problem, when $rss_last is set to the value it works fine:

看起来这最后一部分是问题所在,当 $rss_last 设置为它工作正常的值时:

$rss_last = 'F-Secure vindt malware met certificaat van Maleisische overheid';

When the value came out the RSS it give problems...

当 RSS 值出现时,它会出现问题......

Some more questions..

还有一些问题..

Just found this: Detect encoding and make everything UTF-8

刚刚发现这个:检测编码并使所有内容都为 UTF-8

Best solution? But.. is iconv not more simple, do something like this:

最佳解决方案?但是.. iconv 不是更简单,做这样的事情:

$encoding = some_function_to_get_encoding_from_feed($feed);
$rss_last = iconv($encoding, "UTF-8//TRANSLIT", $feed->channel->item[0]->title);

But what to use for "some_function_to_get_encoding_from_feed"? mb_detect_encoding?

但是“some_function_to_get_encoding_from_feed”用什么?mb_detect_encoding?

And mb_convert_encoding vs iconv?

和 mb_convert_encoding 与 iconv?

采纳答案by Nikolay Baluk

1) There is no global solution.

1) 没有全局解决方案。

2)

2)

AddDefaultCharset UTF-8

It's needed for Apache response to client with right encoding. Make it.

它需要 Apache 以正确的编码响应客户端。做了。

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

not necessarily, but recommended by W3C.

不一定,但 W3C 推荐。

$config['charset'] = 'UTF-8';

it's desirable

这是可取的

$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';

Encoding for CI connection to database. If encoding of your database is UTF-8 - make it mandatory.

CI 连接到数据库的编码。如果您的数据库的编码是 UTF-8 - 使其成为强制性的。

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

Do not do this unless necessary. Charset already indicated in HTML code and .htaccess.

除非必要,否则不要这样做。字符集已在 HTML 代码和 .htaccess 中指示。

Use utf8_unicode_ci or utf8_general_ci for the MySQL database? And why?

For their own language (Russian), I use utf8_general_ci.

对于他们自己的语言(俄语),我使用 utf8_general_ci。

In my editor (Notepad++) save files as UTF-8?

Absolutely! All code that Apache will give as UTF8 should be in UTF8.

绝对地!Apache 将作为 UTF8 提供的所有代码都应该是 UTF8。

How about reading RSS feeds, how to handle multiple charsets?

If you have each RSS in each table - you can specify charset for each table and set right encoding with each sql query. Yes, cyrillic symbols, for example, will fails on non-UTF8.

如果每个表中都有每个 RSS - 您可以为每个表指定字符集并为每个 sql 查询设置正确的编码。是的,例如,西里尔符号将在非 UTF8 上失败。

回答by Shomz

UTF-8 (without BOM) should give you the best results based on your configuration and there's no need to send separate headers since the encoding is already selected in the head part. Utf8_general_ci should do fine for the MySQL database. Perhaps the entries in the database are not valid?

UTF-8(无 BOM)应该根据您的配置为您提供最佳结果,并且无需发送单独的标头,因为编码已在头部部分中选择。utf8_general_ci 对于 MySQL 数据库应该没问题。也许数据库中的条目无效?