php pathinfo \ basename解析带中文路径时不正确

pathinfo \ basename解析带中文路径时不正确

$p1 = pathinfo('atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4');
$p2 = pathinfo('atlas/dirname/sssss#ddddd.mp4');

print_r($p1);
print_r($p2);

在这里插入图片描述

上面的代码直接结果中看到中文的路径解析的时候basenamefilename是不正确的,而英文路径确实是没有问题的。

basename也同样存在这个问题

print_r(basename('atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4'));
echo "\n";
print_r(basename('atlas/dirname/sssss#ddddd.mp4'));

在这里插入图片描述

如何解决

打开官方文档可以看到这个警告

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.

basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function.

这个两个函数都和locale有关系,那么在解析中文的时候我们只要通过setlocale函数设置下就好了。

setlocale(LC_ALL, "zh_CN.UTF-8");

注意

  • 因为受字节数的影响,中文路径情况下上面解析错误的情况不一定复现。例如把上面的路径修改为了atlas/这是文件夹/可爱胖胖是怎么变帅的#超能陆战队.mp4就不会有问题,所以可能会出现一会儿有问题一会儿没有问题的神奇现象。

  • 一定要注意setlocale函数的一个Warning(这个警告在中文文档中没有

    The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS, HHVM or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值