php - UTF-8 日期编码不正确

标签 php utf-8

我在字符字符集方面遇到了困难,我怀疑我的显示日期功能返回非 UTF-8 字符(août 被菱形 août 内的问号替换)。

在本地服务器上工作时一切正常,但是当我将代码推送到临时服务器上时,它无法正确显示。

  • 我的 php 文件保存为 UTF-8 NO BOM
  • 如果我检查输出页面,标题会显示 UTF-8。
  • 我的本地计算机是安装了 MAMP 的 Mac,我的主服务器安装了 CentOS,并安装了 cPanel。

这是我怀疑导致问题的部分:

$langCode = "fr_FR"; /* Alos tried  fr_FR.UTF-8 */
setlocale(LC_ALL, $langCode);
$monthName = _(strftime("%B",strtotime($dateStr)))
echo $monthName; /* Alos tried  utf8_encode($monthName) worked on my staging server but not on my local server ! I'm using  */

最佳答案

终于找到了如何找到错误并修复它。

setlocale(LC_ALL, 'fr_FR');

var_dump(mb_detect_encoding(_(strftime("%B",strtotime($dateStr)))));

转储在本地返回 UTF-8,在临时服务器上返回 FALSE

PHP.net documentation about mb_detect_encoding()

Return Values

The detected character encoding or FALSE if the encoding cannot be detected from the given string.

So charset can't be detected. I will try to force it "again"

setlocale(LC_ALL, 'fr_FR.UTF-8');

var_dump(mb_detect_encoding(_(strftime("%B",strtotime($dateStr)))));

这次转储在本地返回 UTF-8,在登台服务器返回 UTF-8。因此,我回滚代码以查看第一次尝试使用 fr_FR.UTF-8 时发生了什么,为什么它不起作用?我意识到我正在使用 utf8_encode() 就像用户 deceze 在此函数文档的注释中指出的那样,

In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text.

谢谢大家的帮助!

关于php - UTF-8 日期编码不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24895635/

相关文章:

PHP 计算 foreach 循环中的行数

utf-8 - 在 bash 中将转义字符转换为 UTF-8

linux - 如何以编程方式更改文件编码 linux?

php - 如何在 Symfony 5 的其他目录中自动注册 'controllers as services'

PHP 和 odbc 到 mssql - 列名未正确显示

php - 新的 html5 canvas 属性与 javascript 交互

php - 从sql View 中选择时如何修复laravel中的 "Prepared statement needs to be re-prepared"

Javascript -> 下载以 ISO-8859-1/Latin1/Windows-1252 编码的 CSV 文件

java - 如何通过 ResourceBundle 在资源属性中使用 UTF-8

c++ - 如何从 Windows 上的 c++ 控制台应用程序打印 UTF-8