PHP echo Special character problem ("ç")

Asked

Viewed 56,968 times

12

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

c cedilha não reconhecido

I did a short test to follow with the name of the month and it worked correctly, so I guess the problem is in the function date. The code I used is as follows::

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'));
echo strftime( '%A, %d de ' .$uppercaseMonth. ' de %Y', strtotime('today'));
echo (" ....Março");

Solved:

echo utf8_encode(strftime( '%A, %d de ' .$uppercaseMonth. ' de %Y', strtotime('today')));
  • "date_default_timezone_set('Europe/Lisbon')" - this Europe/Lisbon doesn’t interfere? shouldn’t it be something like America/Sao_paulo? http://php.net/manual/en/timezones.america.php

  • 1

    I’m from Portugal, so I think this is the right way

  • 1

    ah! sometimes I forget that we have users who are not Brazilian...

  • Dude, I’ve always had problems with ISO. Always look for start your application already with UTF-8, Voce will not have more problems with accentuation

8 answers

12


No more ISO charset, use UTF-8!

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


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

A possible setlocale, already voted, is the tip of https://stackoverflow.com/a/10927727 , 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, it 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 setting:

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 also needs to be in UTF8(!). Check with a serious and reliable editor (never Windows Notepad!), for example Sublimetext or Textpad.

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

  • 1

    I have had this problem on a server that did not have utf8 locale available for ptbr. I ended up having to give a utf8_encode of the month before leaving.

  • 2

    Nowadays all serious operating systems (up to Windows!) have UTF-8 support. REFUSE, or at least distrust, anything that does not have support for UTF-8.

  • Answer fuck! Helped me a lot in a situation where I needed to read image files from one directory and copy to another, congratulations!

3

Among the tags <head></head> insert:

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 the characters php "encoded" in UTF-8 as UTF-8. This will make it right.

2

Option 1:

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

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

The above two encodings must be equal.

Option 1.1 not elegant

see if she’s in UTF-8 or ISO-8859-1, if I was in utf change to iso if I was in iso switched to utf. See which one works.

Option 2:

use the function htmlentities to convert into html entities

echo htmlentities(" ....Março");
  • Obsessed by the answer... How do I enter the function 'htmlentities()' (based on my code above)? I tried several ways and could not

  • Stay like this echo htmlentities(" ....Março");

2

Solution without gambiarras:

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

To prevent this problem, the ideal is to use UTF-8 as standard encoding in everything that is part of your project:

  • In all your project files;
  • In your database;
  • On the headers sent to the user;
  • If you use an IDE like Eclipse, set the file pattern 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 these kinds of errors once and for all.

2

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

$uppercaseMonth = htmlentities(ucfirst(gmstrftime('%B')));

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 corrects the problem.

php output

0

I will give only one opinion as a complement, since the companions answered wisely earlier.

I always set the option default_charset of php.ini as utf-8. This avoids problems like having to set 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 won’t have access to php.ini and, if the function is disabled ini_set, the solution (by PHP side) is to use the header("Content-type:text/html;charset=utf-8") as cited above.

0

I see that you have not defined the encoding that php itself will work... add this at the top of the page that you should solve:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.