dongna1593 2018-05-14 18:37
浏览 85
已采纳

utf-8 Carbon格式的错误字符

I know there is much about this, but it doesn't work, I have the language installed.

locale -a | grep es
 es_ES
 es_ES.ISO8859-1
 es_ES.ISO8859-15
 es_ES.UTF-8

I have set to utf-8

\Carbon\Carbon::setUtf8(true);
setlocale(LC_ALL, 'es_ES.UTF-8');
$game_date = $date->formatLocalized('%A %d %B %Y %H %M %p');

I have the utf-8 tag in my html

<meta charset="utf-8">

but I am still getting the wrong characters.

"sábado" - "miércoles"

  • 写回答

1条回答 默认 最新

  • duandaiqin6080 2018-06-02 09:49
    关注

    I had the same problem when trying to use the sk_SK.UTF-8 locale. What helped me to solve the problem was to remove the \Carbon\Carbon::setUtf8(true); portion of the code.

    But why does it work like this? Firstly the Carbon documentation regarding the setUtf8 function says this:

    Some languages require utf8 encoding to be printed (locale packages that does not ends with .UTF8 mainly). In this case you can use the static method Carbon::setUtf8() to encode the result of the formatLocalized() call to the utf8 charset.

    Upon examining the source code for Carbon the formatLocalized() function calls utf8_encode() function from the PHP library if we previously set the variable utf8 to true with the already mentioned Carbon::setUtf8(true). Carbon source on GitHub

    return static::$utf8 ? utf8_encode($formatted) : $formatted;
    

    Because your locale is already configured to use the UTF-8 standard the further php encoding messes the formatted string up.

    I figured that if you want to use Carbon to format your strings to utf8 you must first get rid of the UTF-8 encoding when setting your locale with setLocale(). However, I would just stick to removing the Carbon function and using the proper locale.

    TL;DR

    Use correct locale with UTF-8 encoding e.g. es_ES.UTF-8 or use try to use locale without the UTF-8 encoding and rely on \Carbon\Carbon::setUtf8(true); and utf8_encode() function. I suggest the first option. Hope this helped :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么