Laravel的utf8编码问题 [英] Utf8 encoding issue with Laravel
问题描述
在部署laravel网站(在本地正常运行)后,我遇到了问题.有些文字编码不正确,例如:Joël应该是Joël
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
一些信息:
-
并非所有视图都会受到影响,某些变量会在一个视图中正确呈现,并在另一个视图上损坏(这使我认为这不是mysql问题)
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 )
我的json响应不再起作用(格式错误的UTF-8字符,可能编码错误),这使我认为这一定是mysql问题...)
My json response are not working anymore ( Malformed UTF-8 characters, possibly incorrectly encoded ) , which make my think it must be a mysql problem ... )
我已经尝试将其放入我的AppServiceProvider中 \ Blade :: setEchoFormat('e(utf8_encode(%s))');
I already tried to put this in my AppServiceProvider \Blade::setEchoFormat('e(utf8_encode(%s))');
我的strftime也不起作用,但是可以用php的utf8_encode函数来解决,该函数无法与我的其他变量一起使用.例如,如果$ c-> name给出Joël,则utf8_encode($ c-> name)也给出Joël
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
在config/database.php中,字符集设置为utf8mb4,协作设置为utf8mb4_unicode_ci
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 ?
推荐答案
我找到了一个解决方案:
I found a solution :
我使用
{!! htmlentities($variable, ENT_QUOTES, "UTF-8") !!}
但这不方便...
我尝试过:
Blade::setEchoFormat('e(htmlentities(%s,ENT_QUOTES,'UTF-8'))');
在AppServiceProvider中,但不能解决问题.
in AppServiceProvider but it doesn't solve the problem.
所以这是一个临时解决方案...
So it's a temporary solution ...
这篇关于Laravel的utf8编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!