dongrong5189 2018-12-12 08:53
浏览 1236

Laravel的Utf8编码问题

I have an issue after deploying my laravel website ( that works properly in local ). Some text are not encoded correctly, for example : what should be Joël is Joël

Some information :

  • Not all views are affected, some variables are rendered properly in a view and broken on another view ( which make me think it's not a mysql issue )

  • My json response are not working anymore ( Malformed UTF-8 characters, possibly incorrectly encoded ) , which make my think it must be a mysql problem ... )

  • I already tried to put this in my AppServiceProvider \Blade::setEchoFormat('e(utf8_encode(%s))');

  • My strftime were also not working but it can be resolved with utf8_encode function of php, which is not working with my other variables. For example if $c->name gives Joël , utf8_encode($c->name) also gives Joël

  • In config/database.php, charset is set to utf8mb4 and collaction is set to utf8mb4_unicode_ci

Would you have any idea to solve my problem ?

  • 写回答

2条回答 默认 最新

  • douzhang3822 2018-12-12 09:26
    关注

    Just do this in a Service Provider AppServiceProvider and put into boot method

    Blade::setEchoFormat('e(utf8_encode(%s))');
    

    Generally sticking to UTF-8 keeps life simple.

    Be super careful copying and pasting from anywhere else into your code - basically always go through Notepad++ and use its convert to UTF-8 (without BOM) before copying and pasting into your code.

    Then make sure all your views (including error pages), have

    <meta charset="UTF-8">
    

    Or the following if you're doing HTML4

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    

    Hope this will help you

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器