关于php basename()函数的基本使用方法,大家可以查看这篇文章《php basename()解析路径并获取文件名称》
有两种方法可以解决basename()函数无法获取带中文字符的文件名
- 用setlocale()函数设置区域方法
- preg_replace()正则表达式方法
下面具体了解每种方法如何实现basename()支持中文
第一种方法:setlocale()函数设置区域
basename()函数依赖于区域,所以我们需要使用setlocale()为其设置区域
<?php ??setlocale(LC_ALL, ‘zh_CN.GBK‘); ??$path = "/home/www/data/用户.txt"; $filename = basename($path); ?print $filename;?> ?
输出:用户.txt
第二种方法:preg_replace()正则表达式
<?php/* http://www.manongjc.com/article/1296.html */function get_basename($filename){ ????????return preg_replace(‘/^.+[\\\\\\/]/‘, ‘‘, $filename); ???}$path = "/home/www/data/用户.txt";$filename = get_basename($path); print $filename;?>
输出:用户.txt
PHP basename 函数 linux下中文路径的问题解决方法
原文地址:https://www.cnblogs.com/jiangzq/p/9106189.html