简体   繁体   中英

How to set UTF-8 encoding for a PHP file

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.

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

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.

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

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

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

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

试试这种方式header('Content-Type: text/plain; charset=utf-8');

HTML file:

<head>

<meta charset="utf-8">

</head>

PHP file :

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

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM