如何在PHP中检索当前的Windows代码页 [英] How to retrieve the current windows codepage in PHP

查看:103
本文介绍了如何在PHP中检索当前的Windows代码页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中存在一个错误,该错误会处理上载的文件名-请参见 http://bugs.php.net/bug.php?id=47096 .因此,我正在使用一个函数将文件名转换为正确的UTF8.

There exists a bug in PHP which mangles filenames on uploads - see see http://bugs.php.net/bug.php?id=47096 . Therefore I am using a function to convert the filename to proper UTF8.

该功能的重点是:

    if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
            $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
            if ( function_exists( 'iconv' ) ) {
                    $filename = iconv( 'UTF-8', $codepage, $filename );
            } elseif ( function_exists( 'mb_convert_encoding' ) ) {
                    $filename = mb_convert_encoding( $filename, 'UTF-8', $codepage );
            }
    }        

这在较旧的PHP版本(< 5.2)上运行良好.但是,对于最新的PHP版本,该命令

This worked fine with older PHP versions (<5.2). However with recent PHP versions the command

setlocale( LC_CTYPE, 0 )

返回'C'-显然不是Windows代码页,因此转换失败.

returns 'C' - which is obviously not a Windows code page and so the conversion fails.

是否有可靠或替代的方法来检索PHP中的当前Windows代码页?

Is there a reliable or alternative way to retrieve the current Windows code page in PHP?

推荐答案

有趣的是,它适用于旧版本的PHP.如果未显式设置语言环境,则返回值C表示尚未设置语言环境,并且正在使用默认语言.

Interesting that it worked on older versions of PHP. Without explicitly setting the locale, a return value of C indicates that a locale has not been set and the default is being used.

您可以调用setlocale( LC_CTYPE, '' ),这将强制将语言环境显式设置为默认系统语言环境.用空字符串调用该方法的返回值应该是新设置的语言环境(与使用0调用并期望查询该语言环境并无需修改即可返回给您的语言相反).

You could call setlocale( LC_CTYPE, '' ) which will force the locale to be explicitly set to the default system locale. The return value of calling the method with an empty string should be the newly set locale (as opposed to calling with 0 and expecting the locale to be queried and returned to you without modification).

空字符串是一种将当前语言环境显式设置为默认语言环境的方法,而不是仅查询它.

The empty string is a way of explicitly setting the current locale to the default, as opposed to just querying it.

这篇关于如何在PHP中检索当前的Windows代码页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆