PHP declares encoding

On the declare() page of the PHP manual :

Encoding

A script encoding can be specified per-w371> using the encoding directive.

Example # 3 An encoding declaration for a script.

 <?php declare(encoding='ISO-8859-1'); // code here ?> 

(I use both PHP 5.3 and 5.5. Currently, my files are saved in UTF-8, and I'm sending the Content-Type: text/html; charset=utf-8 header Content-Type: text/html; charset=utf-8 when serving HTML files.)

+6
source share
1 answer

PHP 5.6 comes with the new default charset directive installed on UTF-8 , in some cases this may be a problem for pages served in the meta tag as latin1, you can override this directive by calling ini_set('default_charset', 'iso-8859-1') in your scripts.

To do this, place each php file that you want to encode in Latin 1 at the beginning of your script:

example: index.php

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

Then create a “php” folder under your root site and put this piece of code in 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 configured to latin1 ( ISO-8859-1 ) and you want to serve the utf-8 (unicode) page, you can force encode using the same method, but instead use iso-8859-1, utf-8. Look at it:

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 this way!

+3
source

Source: https://habr.com/ru/post/1208285/


All Articles