PHP 5.6 upgrade and special characters

2019-04-28 19:20发布

I have a website where I've used php to include sections rather than having code be duplicated for each page.
However, recently my webhost upgraded the PHP to 5.6, and now all my Æ, Ø and Å's give me the replacement character (�).
I'm not running any databases, and setting a charset in the html didn't help.

I'm very inexperienced with PHP, so I have no idea how to fix it.
Please, any help would be great!

4条回答
The star\"
2楼-- · 2019-04-28 20:03

For now the best thing to do is to downgrade to PHP 5.5 or 5.4, because of what CBroe explained in his commentary saying that PHP 5.6 now sets default charset as UTF-8 and many websites are still not ready for it. I had the same issue after migrating to a new server, so I downgraded to PHP 5.5 and automatically all the special characters are displaying correctly again. If you're using WHM, use EasyApache to rebuild Apache and then select the desired PHP version when prompted.

I personally think that PHP developers should warn users better, I mean, making it clear that upgrading to version 5.6 can bring special characters issues for non UTF-8 ready websites/applications. What they did just imposing UTF-8 to millions of developers worldwide was too fast or even too radical.

查看更多
做个烂人
3楼-- · 2019-04-28 20:10

PHP 5.6 changes the default value of default_charset to UTF-8. The value should better be set.

If the charset of your php files is ISO-8859-1 (all in the directory) you can easy put a .htaccess File in the root directory:

php_value default_charset ISO-8859-1

If only some of your files are in ISO-8859-1 you can easy send a header in the first line of the php file (no need for default_charset):

<?php 
header('Content-Type: text/html; charset=iso-8859-1');
?>

Or convert the encoding of php textfiles to utf8 and don't change defaults.

查看更多
smile是对你的礼貌
4楼-- · 2019-04-28 20:17

If you have multi host web server especially with older websites and multiple languages, for maximum compatibility with earlier PHP versions like 5.4 just force reset default_charset value in /etc/php.../php.ini:

In the global [PHP] section find this key and edit, or put like this:

default_charset = ""

This will not force PHP to set UTF-8 charset for all php files, especially older legacy or national encoding like windows-1251, windows-1257, iso-8859-1, iso-8859-2, ISO-8859-13 and similar.

Or if you are developer, sure use Rob's answer and add code directly to you php application ini_set("default_charset", "");

查看更多
祖国的老花朵
5楼-- · 2019-04-28 20:18

I had the same problem after upgrading from php 5.5. to 5.6.

Solved it by setting the default_charset to an empty string in my script:

ini_set("default_charset", "");

Or if you have acceess to php.ini you can set default_charset = "" instead.

More info: https://www.saotn.org/php-56-default_charset-change-may-break-html-output/

查看更多
登录 后发表回答