2

我有一个开源 PHP 网站,我打算修改/翻译它(主要是常量字符串),以便日本用户可以使用它。

原始代码是PHP+MySQL+Apache,用英文写成charset=utf-8

例如,我想将“登录”一词更改为日语对应的“ログイン”等

我不确定是否必须将 PHP 代码保存为 utf-8 格式(就像 Python 一样)?

我只有 Python 的经验,那么我应该注意哪些其他问题?

4

3 回答 3

2

If it's in the file, then yes, you will need to save the file as UTF-8.

If it's is in the database, you do not need to save the PHP file as UTF-8.

In PHP, strings are basically just binary blobs. You will need to save the file as UTF-8 so the correct bytes are read in. In theory, if you saved the raw bytes in an ANSI file, it would still be output to the browser correctly, just your editor would not display it correctly, and you would run the risk of your editor manipulating it incorrectly.

Also, when handling non-ANSI strings, you'll need to be careful to use the multi-byte versions of string manipulation functions (str_replace will likely botch a utf-8 string for example).

于 2012-06-03T06:56:45.987 回答
0

很抱歉您在使用 Python 后必须使用 PHP。

PHP 没有字符集的概念:所有字符串都是二进制的,即使在解析的 php 代码中也是如此,因此如果您在 php 字符串中包含 UTF-8 多字节字符,请确保代码文件中的字节为 UTF-8 字节。

您需要非常小心地在应用程序的各个级别使用字符串函数。您还需要确保您的 MySQL 连接设置为使用 UTF-8(在 PDO 的更高版本中使用SET NAMEScharsetdsn 参数),并且您的 mysql 字符串列数据类型使用 utf-8 存储。

于 2012-06-03T07:33:12.487 回答
0

If the file contains UTF-8 characters then save it with UTF-8. Otherwise you can save it in any format. One thing you should be aware of is that the PHP interpreter does not support the UTF-8 byte order mark so make sure you save it without that.

于 2012-06-03T06:58:42.150 回答