我在字符字符集方面遇到了困难,我怀疑我的显示日期功能返回非 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 在此函数文档的注释中指出的那样,
谢谢大家的帮助!
关于php - UTF-8 日期编码不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24895635/