[英]UTF-8 dates doesn't encode properly
I have hard time with character charset, I suspect my fonction that display date to return non UTF-8 character (août is replaced by a question mark inside a diamond
août
). 我在使用字符字符集时遇到了
麻烦
,我怀疑我的功能是显示日期以返回非UTF-8字符(在
aot中显示为
问号)。
When working on my local server everything's fine but when I push my code on my staging server, it's not displaying properly. 当在本地服务器上工作时,一切都很好,但是当我在暂存服务器上推送代码时,它无法正确显示。
Here is the part I suspect causing problem : 这是我怀疑引起问题的部分:
$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 */
Finally found how to find the bug and fix it. 终于找到了如何查找错误并修复它。
setlocale(LC_ALL, 'fr_FR');
var_dump(mb_detect_encoding(_(strftime("%B",strtotime($dateStr)))));
the dump returned UTF-8
on local and FALSE
on staging server. 转储在本地服务器上返回UTF-8
,在登台服务器上返回FALSE
。
PHP.net documentation about mb_detect_encoding() 有关mb_detect_encoding()的PHP.net文档
Return Values ¶ 返回值
The detected character encoding or FALSE if the encoding cannot be detected from the given string. 如果无法从给定的字符串中检测到编码,则检测到的字符编码或为FALSE。
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)))));
this time the dump returned UTF-8
on local and UTF-8
on staging server. 此时转储返回UTF-8
的本地和UTF-8
的临时服务器。 So I rollback my code to see what's happened when I tried first time with fr_FR.UTF-8 why does it was not working ? 因此,我回滚了我的代码,以查看当我第一次尝试使用fr_FR.UTF-8时发生了什么,为什么它不起作用? And I realize I was using utf8_encode()
like pointed by user deceze in comment of this function's doc, 而且我意识到我正在使用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. 实际上,将此功能应用于未使用ISO-8859-1编码的文本很可能会使该文本乱码。
Thank you for your help everyone ! 谢谢大家的帮助!
将此元标记放在<head></head>
内的html代码上
<meta charset="UTF-8">
It seems your server are configured to send the header 看来您的服务器已配置为发送标头
content-type: text/html; charset=UTF-8
as default. 默认情况下。 You could change your server configuration or you could add at the very start 您可以更改服务器配置,也可以一开始添加
<?php
header("content-type: text/html; charset=UTF-8");
?>
to set this header by yourself. 自行设置此标头。
you need to use : 您需要使用:
<?php
$conn = mysql_connect("localhost","root","root");
mysql_select_db("test");
mysql_query("SET NAMES 'utf8'", $conn);//put this line after you select db.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.