我最近通过 apt-get 在我的 Ubuntu 12.10 上安装了 PHP 5.4。
PHP 信息显示:PHP 版本 5.4.6-1ubuntu1
我刚刚安装了所有常用包,如 mysql、pgsql、curl 等,没有进行任何其他更改,但我遇到了问题。
我喜欢在我的文件和数据库中使用 ISO-8859-1/latin1 编码,因为这是我获得最佳工作流程的地方。现在我遇到了一个问题,因为 PHP 似乎无法处理消息以这种方式编码的异常。
好吧,为了更好地说明这一点,我创建了一个这样的测试文件:
ini_set('display_errors', 1);
error_reporting(E_ALL);
throw new Exception('é');
如果上面的代码在一个 utf-8 文件中,没关系,启用 Xdegub 我得到:
( ! ) Fatal error: Uncaught exception 'Exception' with message 'é' in /home/henrique/public/teste.php on line 5
( ! ) Exception: é in /home/henrique/public/teste.php on line 5
Call Stack
# Time Memory Function Location
1 0.0002 124212 {main}( ) ../teste.php:0
如果文件是 ISO-8859-1,如果启用了 Xdebug,问题只是没有显示消息:
( ! ) Fatal error: in /home/henrique/public/teste.php on line 5
( ! ) Exception: in /home/henrique/public/teste.php on line 5
Call Stack
# Time Memory Function Location
1 0.0002 124436 {main}( ) ../teste.php:0
但是,如果没有 Xdebug,我得到的只是这条“非常明确”的信息:
Fatal error: in /home/henrique/public/teste.php on line 5
也许这是 Apache 中的问题,因为当我使用命令行尝试相同的操作时,我得到:
Stack trace:
#0 {main}
thrown in /home/henrique/public/teste.php on line 5
Fatal error: Uncaught exception 'Exception' with message '�' in /home/henrique/public/teste.php on line 5
Exception: � in /home/henrique/public/teste.php on line 5
Call Stack:
0.0002 121256 1. {main}() /home/henrique/public/teste.php:0
信息还在,但是,已经看不清了,但是是不是...
编辑
我也尝试了 Lighttpd 1.4.28,结果是一样的。
编辑2:
尝试使用 PHP 5.4 内置服务器并在我的终端上得到这个:
[Wed Jun 5 21:32:08 2013] PHP Fatal error: Uncaught exception 'Exception' with message '�' in /var/www/test2.php:9
Stack trace:
#0 {main}
thrown in /var/www/test2.php on line 9
[Wed Jun 5 21:32:08 2013] 127.0.0.1:55116 [200]: /test2.php - Uncaught exception 'Exception' with message '�' in /var/www/test2.php:9
Stack trace:
#0 {main}
thrown in /var/www/test2.php on line 9
但是在浏览器中,还是一样的问题。
最佳答案
你在不同的服务器上试过吗?
我认为是你的配置,我在我的服务器上创建了一个测试文件,你可以在这里查看 http://cai.tlacaelelrl.com/tests/test.php
内容是
ini_set('display_errors', 1);
error_reporting(E_ALL);
print 'Character encoding is: '.mb_internal_encoding();
throw new Exception('é');
字符集应用到文件中,我在htaccess文件中也添加了字符集。
我不确定是不是因为 xdebug,但我无法在启用它的情况下进行测试。
你可以尝试添加这个吗
AddCharset ISO-8859-1 .php
到您的 .htaccess 文件
关于PHP 5.4 抛出异常 - 看不到带有 ISO-8859-1 编码字符串消息的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13220909/