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

2020-04-08 16:21:31 +08:00
 s609926202



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

本地环境是:7.3.12 ZTS

3165 次点击
所在节点    PHP
5 条回复
littleylv
2020-04-08 16:29:24 +08:00
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
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
2020-04-09 14:32:27 +08:00
@encro @littleylv 设置了一下在我的本地 windows 还是有错误
https://i.loli.net/2020/04/09/STD7CMz6ytgfwrd.jpg
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
2020-04-21 21:51:43 +08:00
@LiSkyAir 赞,这隐秘的配置

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/660490

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX