Setting the PHP Default Encoding to Utf-8

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.

How to best configure PHP to handle a UTF-8 website

The supposed issues of PHP with Unicode content have been somewhat overstated. I've been doing multilingual websites since 1998 and never knew there might be an issue until I've read about it somewhere - many years and websites later.

This works just fine for me:

Apache configuration (in httpd.conf or .htaccess)

AddDefaultCharset utf-8

PHP (in php.ini)

default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=6

MySQL

CREATE your database with an utf8_* collation,
let the tables inherit the database collation and
start every connection with "SET NAMES utf8"

HTML (in HEAD element)

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

How to set UTF-8 encoding for a PHP file

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

PHP declare encoding

PHP 5.6 comes with a new default charset directive set to UTF-8, in some case this may be a problem with pages served in metatag as latin1, you can override this directive by calling ini_set('default_charset', 'iso-8859-1') in your scripts.

For doing that put on each php file you want to be coded to latin1 this piece of code at the beginning of your scripts:

example: index.php

<?php
$server_root = realpath($_SERVER["DOCUMENT_ROOT"]);
$config_serv = "$server_root/php/config.php";
include("$config_serv");
?>

Then create a folder "php" under your root website and put this piece of code into config.php:

example: config.php

<?php
##########################################################################
# Server Directive - Override default_charset utf-8 to latin1 in php.ini #
##########################################################################
@ini_set('default_charset', 'ISO-8859-1');
?>

If your php.ini is set to latin1 (ISO-8859-1) and you want serve a utf-8 (unicode) page you can force encoding using the same way but putting instead of iso-8859-1, utf-8. Look at that:

example: config.php

<?php
##########################################################################
# Server Directive - Override default_charset latin1 to utf-8 in php.ini #
##########################################################################
@ini_set('default_charset', 'UTF-8');
?>

I hope you find my answer useful, I solved my problem in this way!

How to change charset of my website to utf8?

Try adding these 2 lines on top of your .htaccess:

AddDefaultCharset UTF-8
AddCharset UTF-8 .htm .html .css .js .php

Also try adding this in your HTML's <head> section:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

Do I need to set ini_set( 'default_charset', 'UTF-8' );?

No, you don't have to.

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

sets this for every page already

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 to set HTTP header to UTF-8 using PHP which is valid in W3C validator

Use header to modify the HTTP header:

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

Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can’t change it any more. You can check that with headers_sent. See the manual page of header for more information.



Related Topics



Leave a reply



Submit