53

I have a PHP script called :

http://cyber-flick.com/apiMorpho.php?method=getMorphoData&word=kot

That displays some data in plain text:

Cz��� mowy: rzeczownik
Przypadek: dope�niacz
Rodzaj: şe�ski
Liczba: mnoga

As you can see in place of proper chars there are so "bushes". What i would like to do is display this in a way so that people see in browser proper UTF-8 characters.

You can encapsulate it in HMTL tags and set in meta UTF-8 encoding, but because the data received from this script will be processed further I don't want to use any HTML tags, it should be only plain text result set.

So is there a way to inform browser that this file is UTF-8 without using meta tags?

PS. File is encoded in UTF-8 and if I manually change charset encoding in my browser to UTF-8 it displays ok, but what I want to acomplish is people to not be required to do so.

Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236
  • Possible duplicate of [Set HTTP header to UTF-8 using PHP](http://stackoverflow.com/questions/4279282/set-http-header-to-utf-8-using-php) – Mike Oct 09 '15 at 21:25

7 Answers7

113
header('Content-type: text/plain; charset=utf-8');
Christian Studer
  • 24,947
  • 6
  • 46
  • 71
delphist
  • 4,409
  • 1
  • 22
  • 22
  • 7
    This is the correct way. `Content-Type` is normally capitalized, by the way. (Does this make a difference?) – awm Feb 20 '11 at 11:22
  • 2
    I am sorry, it is an IIS related issue - some people have reported lowercase headers not working properly – Jan Dragsbaek Feb 20 '11 at 11:32
15

Also note that setting a header to "text/plain" will result in all html and php (in part) printing the characters on the screen as TEXT, not as HTML. So be aware of possible HTML not parsing when using text type plain.

Using:

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

Can return HTML and PHP as well. Not just text.

Larry Judd
  • 341
  • 3
  • 4
10

PHP, by default, always returns the following header: "Content-Type: text/html" (notice no charset), therefore you must use

<?php header('Content-type: text/plain; charset=utf-8'); ?>
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
5

You have to specify what encoding the data is. Either in meta or in headers

header('Content-Type: text/plain; charset=utf-8');
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • @tomaszs: uhm, where i missed something? I've written all possible places to specify encoding. It is meta and headers. – zerkms Feb 20 '11 at 11:28
  • 1
    yeah, i write meta is out of question and you wrote i should use meta. – Tom Smykowski Feb 20 '11 at 11:34
  • @tomaszs: I wrote you have to choose either of them, depending on circumstances. And I've read all the question, that is why I gave a sample with header, and nothing about meta. – zerkms Feb 20 '11 at 11:36
  • your first answer was without example till someone posted it. So please don't write you read it cause you didn't. You just could write ok- i didn't wrote and it would be fine. But now you just go the wrong way. Never mind. – Tom Smykowski Feb 20 '11 at 12:28
4

Try this way header('Content-Type: text/plain; charset=utf-8');

Intrepidd
  • 19,772
  • 6
  • 55
  • 63
2

HTML file:

<head>

<meta charset="utf-8">

</head>

PHP file :

<?php header('Content-type: text/plain; charset=utf-8'); ?>
1

Html:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="x" content="xx" />

vs Php:

<?php header('Content-type: text/html; charset=ISO-8859-1'); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta name="x" content="xx" />
jimmy5
  • 47
  • 3