找回密码
 新建账号

PHP Could not convert the script from the detected encoding "UTF-32BE"

[复制链接]
php 发表于 2017/11/30 01:07 | 显示全部楼层 |阅读模式
PHP Fatal error:  Could not convert the script from the detected encoding "UTF-32BE" to a compatible encoding in Unknown on line 0
最近PHP老是报错,始终找不到原因,初步怀疑是Discuz!某个代码文件中出现了UTF-32BE编码的乱码,也许是Discuz!被注入了非法代码文件?
经过无数次的摸索和搜索,最终发现有可能是php.ini里面的zend.multibyte导致了这个问题,该配置项默认是关闭的,设置为On打开它可以支持SJIS, BIG5等编码方式,使用这类编码存储的PHP文件有可能无法被PHP正确识别解析,通过declare(encoding='***')设置编码即可使用指定的编码方式解析PHP脚本,如果zend.multibyte被关闭,使用了SJIS等编码的PHP脚本可能无法正常解析,导致源文件原样输出,或者导致declare(encoding='***')被PHP解析器忽略,并且触发warning级别错误:Warning: declare(encoding=...) ignored because Zend multibyte feature is turned off by settings in path\to\file.php on line 2.
世界上的人类语言太多,编码使人头痛,使用UTF-8不会出现这种问题,建议还是统一使用UTF-8避免出现各种诡异现象……
关于zend.multibyte php手册文件中有如下说明。
zend.multibyte enables parsing of source files in multibyte encodings. Enabling zend.multibyte is required to use character encodings like SJIS, BIG5, etc that contain special characters in multibyte string data. ISO-8859-1 compatible encodings like UTF-8, EUC, etc do not require this option. Enabling zend.multibyte requires the mbstring extension to be available.
一个有意思的问题:如果文件编码是GBK,以下PHP代码运行将报错Parse error: in path\to\file.php on line 2
  1. <?php
  2.         echo('洪仁玕');
  3. ?>
复制代码
如果把单引号换成双引号会报错Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in path\to\file.php on line 3
  1. <?php
  2.         echo("洪仁玕");
  3. ?>
复制代码
解决方法是在php.ini中增加一句代码
  1. <?php
  2.         declare(encoding='cp936');
  3.         echo("洪仁玕");
  4. ?>
复制代码
declare(encoding='***')必须是第一句PHP脚本中的第一句有效代码,否则会报错Fatal error: Encoding declaration pragma must be the very first statement in the script in path\to\file.php on line 3.

手机版|轻松E站

GMT+8, 2024/4/26 16:09

快速回复 返回顶部 返回列表