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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决