当我运行此脚本时:
<?php
$old_locale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, $old_locale);
我明白了:
PHP Warning: setlocale(): Specified locale name is too long in /tmp/loc.php on line 3
PHP Stack trace:
PHP 1. {main}() /tmp/loc.php:0
PHP 2. setlocale() /tmp/loc.php:3
如果我也打印$old_locale
,它看起来像是:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=en_US.UTF-8;LC_ADDRESS=en_US.UTF-8;LC_TELEPHONE=en_US.UTF-8;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=en_US.UTF-8
我想最基本的“为什么”是我试图传入确实太长的语言环境字符串。在ext/standard/string.c
我看到:
if (Z_STRLEN_PP(plocale) >= 255) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Specified locale name is too long");
break;
}
PHP版本:
PHP 5.4.13 (cli) (built: Mar 15 2013 11:27:51)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.2, Copyright (c) 2002-2013, by Derick Rethans
操作系统:最新的CentOS版本6.4(最终版)
从命令行输出locale
:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
我在使用Goodby, CSV时遇到了这个PHP警告。
答案 0 :(得分:3)
set local的第二个参数是字符串或数组,我认为你必须分离你的$ old_locale才能工作:
<?php
$old_locale = setlocale(LC_ALL, '0');
$bits = explode(';',$old_locale);
$loc = array();
foreach($bits as $bit) {
$value = explode("=",$bit);
$loc[] = $value[1];
}
setlocale(LC_ALL, $loc) ;
答案 1 :(得分:1)
$old_locale = explode(";", $old_locale);
$old_locale = $old_locale[0];//returns LC_CTYPE=en_US.UTF-8
$old_locale = explode("=", $old_locale);
$old_locale = $old_locale[1];//returns en_US.UTF-8
setlocale(LC_ALL, $old_locale);