0

I have been trying to print some characters on my php page and it returns something else, like corrupted characters

like

"joão", ø, ç, etc

echo '<br>não<br>'

the return is

não

instead:

não

is this a problem with econding utf-8?

i have tried this code

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

but no result

Steve
  • 1,553
  • 2
  • 20
  • 29

5 Answers5

1

You have a typo in your Content-Type header. It's ...; charset=... with a semicolon.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • header('Content-Type: text/html; charset-utf-8');? – Vítor Campeã Nov 30 '13 at 12:36
  • 1
    `header('Content-Type: text/html; charset=utf-8');` – deceze Nov 30 '13 at 12:36
  • that worked, but if i need to print some chinese characters, there is some global enconding? – Vítor Campeã Nov 30 '13 at 12:40
  • Since you're using UTF-8, that is a "global" encoding which supports Chinese. If your file is saved as UTF-8 and you're setting the above header, `echo "漢字";` works just fine. – deceze Nov 30 '13 at 12:41
  • damn, it realy works, thanks m8, cant give correct asnwer yet, i must wait 5 min :p – Vítor Campeã Nov 30 '13 at 12:44
  • I'll recommend you this for further details: [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – deceze Nov 30 '13 at 12:45
1

Note: Your header header('Content-type: text/html, charset=utf-8');, is the correct header('Content-type: text/html; charset=utf-8');

Your document (php-file) should be saved in utf8 (without bom) or use iso-8859-1 instead of utf8


To save the document in utf8 without boom use notepade++ (select "Convert to utf-8 without BOM"): enter image description here

or use:

header('Content-Type: text/html; charset=iso-8859-1');

Tip (if you are using database):

If your database is UTF-8, then:

  • php file converted to utf-8 without bom
  • set header to utf8 header('Content-type: text/html; charset=utf-8');

If your database is latin1, then:

  • php file converted to ANSI
  • set header to iso-8859-1header('Content-type: text/html; charset=iso-8859-1');
Protomen
  • 9,471
  • 9
  • 57
  • 124
0

Check that your PHP source file is encoded in UTF-8.

gontrollez
  • 6,372
  • 2
  • 28
  • 36
0

try this

header('Content-Type: text/html; charset=iso-8859-1');
Bhadra
  • 2,121
  • 1
  • 13
  • 19
-2

The encoding of your php file itself may be wrong. You might want to change it to utf-8.

However, it is not good practice to include special characters directly in your code. Instead, use the HTML charcode equivalents to produce valid output.

Example

echo 'n&atilde;o';

A comprehensive list of HTML character codes can be found here

SquareCat
  • 5,699
  • 9
  • 41
  • 75
  • -1 There's nothing wrong whatsoever with using "special characters" (what's so "special"?!) directly. It's only annoying to work with a page full of entities. – deceze Nov 30 '13 at 12:35
  • I assumed it was bad practice. Perhaps a remnant from earlier days where output in some browsers didn't work out for german characters? – SquareCat Nov 30 '13 at 12:36
  • A remnant from earlier days where Unicode wasn't very widely supported, especially by the authoring tools, and/or nobody knew how to handle encodings correctly. – deceze Nov 30 '13 at 12:37
  • I see. Thanks for clearing it up. !Thanks for the useless downvote. – SquareCat Nov 30 '13 at 12:38
  • its only for study porpouses, but if i had a big page would be a pain in the ass :P, thanks anywya ^^ – Vítor Campeã Nov 30 '13 at 12:41