8

I'm facing a charset problem here. I'm developing an app that uses a sql server database. The database was not created for this app, it exists before it and works very well. I can't change anything on the database because its too large and its used by many other apps.
I've been finished the auth of my laravel 5 app, so I'll create a view and show in this view the name of logged user. The name is: ADMINISTRADOR DA ACENTUAÇÃO. It use some special characters.
In my views:

{!!Auth::user()->name!!} 

it shows:

ADMINISTRADOR DA ACENTUA��O

But in my controller, before I return the view, I did:

die(\Auth::user()->name);

and it shows me:

ADMINISTRADOR DA ACENTUAÇÃO

I try now do it in my view file:

    {!!Auth::user()->name!!}
<?php die();

And this works fine. It shows me:

ADMINISTRADOR DA ACENTUAÇÃO

It makes me believe the error occours for something laravel does after the views are parsed.

I don't know why it works well when I die the user name on the controller, but not works when I echo its name on the view.

May anyone help me plz?

PS:

  • My view file is using utf8 charset
  • I tried to echo with and without html tags and charset meta. The problem occours on both cases
  • I tried to delete my view file and create a new one with utf8 charset. It doesn't work.
  • I tried to use <?php echo Auth::user()->name; ?> instead blade tags. It doesn't work.
Anderson Silva
  • 709
  • 1
  • 7
  • 31
  • Check the encoding of your view files and try to convert them with a tool like Notepad++, after securing a backup first of course. It might be your IDE that causing the problem, configure your ide to save your files using UTF-8 encoding. Some others reasons might be your server configuration or even worse your database. – Harry Geo May 22 '15 at 14:33
  • I tried to convert my view files, I tried even delete and make a new view file using the utf8 charset. But it doesn't work. – Anderson Silva May 22 '15 at 15:41
  • What your DB has as charset? Meta tag set in the view? – toesslab May 22 '15 at 16:33
  • Its indifferent. I tried using a view without any tag, only the text. If I put a die after the echo, it works, but if I only put the echo, it not works. I think its caused by something Laravel does after view is rendered – Anderson Silva May 22 '15 at 16:39
  • 1
    The best way to check what's happening is to check what is the encoding of your string with [`mb_detect_encoding($str)`](http://php.net/manual/en/function.mb-detect-encoding.php), Make sure it's UTF8. Also note that `die()` and `echo` could have different behaviours. I remember that at one point when I used `die` the contents were correctly printed in the browser but only because the browser was managing the conversion. A not-so-good-fix is to use something to convert your strings to UTF8 ([check this answer](http://stackoverflow.com/a/3479832/908174)) – Luís Cruz May 22 '15 at 17:49
  • I tried now: `{!!mb_detect_encoding(Auth::user()->nomePessoa).' - '.Auth::user()->nomePessoa!!}` but it shows me: UTF-8 - ADMINISTRADOR DA ACENTUA��O – Anderson Silva May 22 '15 at 18:06
  • It's a long shot but try escaping it `{{ Auth::user()->name }}` – user1669496 May 22 '15 at 19:57
  • When I try to escape it, the string is not showed. I change the charset of my view to iso-8859-15 and it worked well when I echo the string unescaped. But now when I echo the escaped string, it isn't showed... (only when it has special chars) – Anderson Silva May 22 '15 at 20:13
  • 1
    Have you tried it in different browsers? Also, are there any special characters later in the view that are forcing the browser to use a different character set? Perhaps an apostrophe type on a Mac, or something like that? – Brian H. May 23 '15 at 04:42
  • Yeah I tried. I change the charset of document to windows-1252 and it works perfect when I echo the variable without escape it works perfect, but when I escape it before echo, it doesn't appears. Now I must alter the behavior of the blade escape function. – Anderson Silva May 25 '15 at 11:45

4 Answers4

4

I solved this issue using this aswer. I've just went in my AppServiceProvider and put into boot method:

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

I don't know if this is the correct way to do it, but it works for me.

Community
  • 1
  • 1
Anderson Silva
  • 709
  • 1
  • 7
  • 31
2

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.

First question - how is the data getting into your database?

Mega Top Tip If you're using a form, make sure you've set the accepted charset attribute on the form element:

<form accept-charset="UTF-8"

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">
Dave Amphlett
  • 1,922
  • 1
  • 15
  • 17
  • So you've got that accept-charset in the form ? That get's the browser to do the conversion to UTF-8 for you before the form data is submitted – Dave Amphlett May 27 '15 at 14:33
  • I wasn't using any form. I was just echoing the name of logged user on the screen.. – Anderson Silva May 27 '15 at 14:55
  • Seems to me the problem is you've got 'incorrectly' coded data in the database - that's where the problem happened - using utf8_encode() in the view is a sticking-plaster, not a solution. Make sure you've got the accept-charset on all your forms and you shouldn't have crappy data in the wrong character. ;) – Dave Amphlett May 28 '15 at 13:00
  • The database I use is a large database and the app I'm developing now is just one module of a bigger app. Our database is full of millions of rows in each table and has more than 200 tables.. I can't change the way data is saved on the database because we have too many other apps saving data in there. We have delphi, java, php and even C# apps saving data in the same database. All of them save the same way and has no problem to retrieve data. – Anderson Silva May 28 '15 at 14:16
  • 1
    fair enough - it's about the only situation where doing utf8_encode() in the view is the right thing to do - to overcome the shortcomings of the other apps if they aren't enforcing a fixed characterset. Watch out if you do any API's as well, since API clients will expect consistent character encoding so you'll probably need to explicitly utf8 there too. – Dave Amphlett May 28 '15 at 17:31
0

Since successful and error response do not behave consistently, the difference may be in the HTTP headers. If that's the case, what happens is that Sqlserver and your browser use the same encoding, likely some Windows default, like cp1252, while Laravel is configured to respond with a different charset than the one used in the database.

Check the HTTP header content-type both when you invoke the view and when you die() - if that is your issue configure Laravel to send some header that agrees with your templates and your database encoding (or of course! transcode explicitely when needed)

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

I accidentally opened my UTF-8 encoded file in different text editor and saved. However, it automatically saved my UTF-8 file as ANSII. So i got to open the file again and save file as UTF-8 encoded.

Fusion
  • 5,046
  • 5
  • 42
  • 51