V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
s609926202
V2EX  ›  PHP

PHP 的 pathinfo 函数文件名中返回路径,是环境问题吗?

  •  
  •   s609926202 · 2020-04-08 16:21:31 +08:00 via iPhone · 3154 次点击
    这是一个创建于 1472 天前的主题,其中的信息可能已经有所发展或是发生改变。

    V2erForiOS

    在其他环境下跑同样的代码,结果又是正常的: https://3v4l.org/OMDeo

    本地环境是:7.3.12 ZTS

    第 1 条附言  ·  2020-04-26 08:49:29 +08:00
    windows (我的是 windows 2004 )下,除了 @LiSkyAir 说的需要设置系统时区外,还需要设置:setlocale(LC_ALL, 'zh_CN.UTF-8');
    5 条回复    2020-04-21 21:51:43 +08:00
    littleylv
        1
    littleylv  
       2020-04-08 16:29:24 +08:00   ❤️ 3
    pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.

    来源: https://www.php.net/manual/zh/function.pathinfo.php#refsect1-function.pathinfo-notes
    encro
        2
    encro  
       2020-04-08 19:30:16 +08:00
    评论里有:


    Note:

    pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.

    Reality:
    var_dump(pathinfo('中国人 2016.xls'));
    exit();
    array(4) { 'dirname' => string(1) "." 'basename' => string(8) "2016.xls" 'extension' => string(3) "xls" 'filename' => string(4) "2016" }

    Expect(Solve):
    setlocale(LC_ALL, 'zh_CN.UTF-8');
    var_dump(pathinfo('中国人 2016.xls'));
    exit();
    array(4) { 'dirname' => string(1) "." 'basename' => string(17) "中国人 2016.xls" 'extension' => string(3) "xls" 'filename' => string(13) "中国人 2016" }
    s609926202
        3
    s609926202  
    OP
       2020-04-09 14:32:27 +08:00 via iPhone
    @encro @littleylv 设置了一下在我的本地 windows 还是有错误
    https://i.loli.net/2020/04/09/STD7CMz6ytgfwrd.jpg
    LiSkyAir
        4
    LiSkyAir  
       2020-04-14 16:23:25 +08:00
    @s609926202
    在 PHP 手册 https://www.php.net/manual/zh/function.setlocale.php#refsect1-function.setlocale-parameters 里边是有说明的

    > **Note**:
    > 在 Windows 中,setlocale(LC_ALL, '')要从系统中的区域 /语言设置(通过控制面板访问) 。

    你如果用的是 Windows 10 的话可以通过 `控制面板 > 时钟和区域 > 更改日期、时间或数字格式 > 管理 > 更改系统区域设置` 勾选 `Beta 版: 使用 Unicode UTF-8 提供全球语言支持` 来解决这个问题
    s609926202
        5
    s609926202  
    OP
       2020-04-21 21:51:43 +08:00
    @LiSkyAir 赞,这隐秘的配置
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2811 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:23 · PVG 10:23 · LAX 19:23 · JFK 22:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.