How to Set Utf-8 Encoding for a PHP File

How to set UTF-8 encoding for a PHP file

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

how to set utf-8 character encoding in php file

First: use php header code before any html - if you want. I still don't know why you're using header() function to set html encoding. It is sufficient if you enter meta tag:

<?php header("Content-type: text/html; charset=utf-8"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color:#F9F0EB;">
<form action="contact.php" method="post" accept-charset="utf-8">
<p>Ваше имя:<input type="text" name="name" /></p>
<p>E-mail:<input type="text" name="email" /></p>
<p>Тема:<input type="text" name="subject" /></p>
<p>Сообщение:<br />
<textarea name="message" rows="5" cols="45"> </textarea></p>
<p><input type="submit" value="Отправить"></p>
</form>
</body>
</html>

But it all applies to the HTML page. For encoding email use this code below:

// set header
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";

// Send email
mail($to, $subject, $message, $headers);

More information here: http://php.net//manual/en/function.mail.php

How can I write a file in UTF-8 format?

file_get_contents() and file_put_contents() will not magically convert encoding.

You have to convert the string explicitly; for example with iconv() or mb_convert_encoding().

Try this:

$data = file_get_contents($npath);
$data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
file_put_contents('tempfolder/' . $a, $data);

Or alternatively, with PHP's stream filters:

$fd = fopen($file, 'r');
stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
stream_copy_to_stream($fd, fopen($output, 'w'));

Setting the PHP default encoding to UTF-8

You should set your default_charset to UTF-8:

default_charset = "utf-8"

(PHP Cookbook may have a typo in it if they ask you to change the default_encoding — I've never heard of it.)

You'll also want to make sure that your web server is set to output UTF-8 if you're going to outputting UTF-8 encoded characters. In Apache, this can be set by in the httpd.conf file:

AddDefaultCharset UTF-8

As for modifying the iconv, exif, and mssql encoding settings, you probably don't need to set these (your settings have these commented out anyhow), but it's a good idea to change them all to UTF-8 anyhow.

Write a file with encoding UTF-8 in php

If your 3rd party program "do not support files in ANSI but UTF-8" as you mentioned in a comment then most likely it's expecting a BOM.

While the Unicode Standard does allow a BOM in UTF-8,[2] it does not
require or recommend it.[3] Byte order has no meaning in UTF-8[4] so a
BOM serves only to identify a text stream or file as UTF-8.

The reason
the BOM is recommended against is that it defeats the ASCII
back-compatibility that is part of UTF-8's design.

So strictly speaking your 3rd party program isn't completely compliant with the standard because the BOM should be optional. ANSI is 100% valid UTF-8 and that is one of the main drivers of it. Anything that can understand UTF-8 accordng to the standard by definition also understands ANSI.

Try writing "\xEF\xBB\xBF" to the front of the file and see if that solves your problem.

How to set text file encoding in PHP?

PHP does not apply an encoding when storing text in a file: it stores data exactly as it is laid out in the string.

You mention that you have problems opening the file in notepad.exe. That text editor is not very good at guessing the encoding of the file you are opening; if the text is encoded in UTF-8 you must choose to open it as UTF-8. Use another text editor if possible. Notepad++ is a popular replacement.

If you must use notepad.exe, as a last resort, write a Byte Order Mark to the file before you write anything else; this will make it recognize the file as UTF-8 while potentially making the file unusable for other purposes (see the Wikipedia article for details).

file_put_contents("file.txt", "\xEF\xBB\xBF" . $data);

how to get utf 8 encoded file with php

You need to convert it to UTF8 yourselves. To do that use mb_convert_encoding() and mb_detect_encoding() PHP functions.

Like this,

$html=file_get_contents('somewhere/somewhat.html');
$html=mb_convert_encoding($html, 'UTF-8',mb_detect_encoding($html, 'UTF-8, ISO-8859-1', true));
echo $html;

mb_convert_encoding() converts character encoding

mb_detect_encoding() detects character encoding



Related Topics



Leave a reply



Submit