如何为PHP文件设置UTF-8编码

时间:2023-01-05 18:51:27

I have a PHP script called :

我有一个PHP脚本,叫做:

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

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.

正如你所看到的,在适当的图表上有“灌木丛”。我想要做的是用一种方式显示它,以便人们在浏览器中看到合适的UTF-8字符。

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.

您可以将它封装在HMTL标记中,并使用元UTF-8编码进行设置,但是由于从该脚本接收的数据将被进一步处理,因此我不希望使用任何HTML标记,因此它应该只是纯文本结果集。

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

那么是否有一种方法可以告知浏览器该文件是UTF-8而不使用meta标记呢?

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.

PS.文件是用UTF-8编码的,如果我在浏览器中手动将字符集编码更改为UTF-8,它会显示ok,但是我想要的是人们不需要这么做。

5 个解决方案

#1


87  

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

#2


8  

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

默认情况下,PHP总是返回以下标题:“Content-Type: text/html”(注意没有字符集),因此必须使用

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

#3


7  

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.

还要注意,将标题设置为“text/plain”将导致所有html和php(部分地)以文本而不是html的形式在屏幕上打印字符。因此,请注意在使用文本类型plain时,可能不会解析HTML。

Using:

使用:

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

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

也可以返回HTML和PHP。不只是文本。

#4


4  

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

试试这个标题('Content-Type: text/plain;charset = utf - 8 ');

#5


3  

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

必须指定数据的编码。要么在元数据中,要么在头数据中

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

#1


87  

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

#2


8  

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

默认情况下,PHP总是返回以下标题:“Content-Type: text/html”(注意没有字符集),因此必须使用

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

#3


7  

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.

还要注意,将标题设置为“text/plain”将导致所有html和php(部分地)以文本而不是html的形式在屏幕上打印字符。因此,请注意在使用文本类型plain时,可能不会解析HTML。

Using:

使用:

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

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

也可以返回HTML和PHP。不只是文本。

#4


4  

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

试试这个标题('Content-Type: text/plain;charset = utf - 8 ');

#5


3  

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

必须指定数据的编码。要么在元数据中,要么在头数据中

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