PHP echo Problem with special characters ("ç") [duplicate]

10

I'm developing a website that displays the names of months with strftime . The html is already with 'charset = UTF-8'. The problem is that the display of the month name appears as in the image below

Ididalittletestbelowwiththenameofthemonthanditworkedcorrectly,soIassumetheproblemisinthedatefunction.ThecodeIusedisthefollowing:

setlocale(LC_ALL,'pt_BR','pt_BR.iso-8859-1','pt_BR.utf-8','portuguese');date_default_timezone_set('Europe/Lisbon');$uppercaseMonth=ucfirst(gmstrftime('%B'));echostrftime('%A,%dde'.$uppercaseMonth.'de%Y',strtotime('today'));echo(" ....Março");

Resolved:

echo utf8_encode(strftime( '%A, %d de ' .$uppercaseMonth. ' de %Y', strtotime('today')));
    
asked by anonymous 07.03.2014 / 13:59

8 answers

7

No longer use ISO charset, use UTF-8!

ISO Latin was retired years ago, W3C has been suggesting use of UTF8 (see RFC-3629 ) in all recommendations. Similarly, for Brazilian websites, e-PING recommendation is the UTF-8 charset standard ... Pattern in fact : idem, UTF-8. If you check large Brazilian portals or even protugueses, you will soon see in the HTML header that the adopted standard is UTF8 (ex. <meta http-equiv="Content-Type"../> source code of UOL ).

Your setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8'...) statement can be by prioritizing ISO and ignoring UTF8 - see what the , "if locale is an array or contains additional parameters, then each element of the array is tried as a location until it succeeds" . I only suggest UTF-8.

A possible setlocale , already voted, is the hint of link , but beware of confusion-Windows,

header('Content-type: text/html; charset=utf-8');    
setlocale(LC_ALL, NULL); // limpa com defaults do sistema... não precisa.
// ERRADO, força Windows setlocale(LC_ALL, 'Portuguese_Brazil.1252');
setlocale(LC_ALL, 'pt_BR.utf-8'); // acho mais correto.

Adapting to your preferences would be something like

header('Content-type: text/html; charset=utf-8');
setlocale( LC_ALL, 'pt_BR.utf-8', 'pt_BR', 'Portuguese_Brazil');
date_default_timezone_set('Europe/Lisbon');

I personally always use the following configuration:

setlocale(LC_ALL,'pt_BR.UTF8');
mb_internal_encoding('UTF8'); 
mb_regex_encoding('UTF8');

Your PHP scripts ... Are UTF8?

Another common problem is your own PHP script, which must also be in UTF8 (!). Check out some serious and reliable publisher (never Windows Notepad!) Such as SublimeText or Textpad .

Ditto databases, XML files, etc. It needs to be all in the same charset , and, easy: just always configure everything with the "universal standard", which is UTF8.

    
07.03.2014 / 15:01
2

For html 4.0 / 4.1 or xhtml 1.0:

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

For html 5:

<meta charset="UTF-8" />

This will define that the characters that php "encoded" in UTF-8 will be understood as UTF-8. What will make it work.

    
07.03.2014 / 14:08
2

Option 1:

See the coding of your php page. There are 2 places to see:

  • When a server (Ex apache, iis) pages are served with a defalut encoding (in this case only apache is utf-8).
  • Encoding of the file, look at which encoding your page was saved (the location of this information will depend on the editor / ide)

The above two encodings must be the same.

Option not elegant

see if it's in UTF-8 or ISO-8859-1 , if I've been in utf change to iso, if I've been in iso mute for utf. See what works.

Option 2:

use the htmlentities function to convert to html entities

echo htmlentities(" ....Março");
    
07.03.2014 / 14:04
2

No gambiarras solution:

This is a common problem caused when using different encodings in the project.

To prevent this problem, it is best to use UTF-8 as the default encoding for everything that is part of your project:

  • All files in your project;
  • In your database;
  • In headers sent to the user;
  • If you use some IDE as Eclipse, set the default of the files for the new encoding as well.

With this, you make sure that any text that goes to the user is consistent and speaking the same "language", preventing such errors once and for all.

    
07.03.2014 / 14:44
2

To use htmlentities() in your case, do the following:

$uppercaseMonth = htmlentities(ucfirst(gmstrftime('%B')));
    
07.03.2014 / 15:39
0

Adding only setlocale(LC_ALL, 'pt_BR.utf8'); instead of setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese' ); already fixes the problem.

    
09.03.2014 / 03:30
0

I see that you have not defined the coding that php itself will be working on ... add it to the top of the page it should resolve:

header('Content-Type: text/html; charset=UTF-8');
    
07.03.2014 / 14:09
0

I will give only one option as a complement, since the companions have answered wisely before.

I always set the default_charset of php.ini option to utf-8 . This avoids problems like having to be setting up encoding directly in the code.

What you can also do is use ini_set('default_charset', 'utf-8') at the beginning of the document.

The problem in this case is that, if your server is shared, you probably will not have access to php.ini , and if the ini_set function is disabled, the solution ) is to use header("Content-type:text/html;charset=utf-8") as quoted above.

    
20.06.2014 / 17:52