The basic use of PHP basename () function, you can see this article "PHP basename () parse path and get the file name"
There are two ways to resolve the basename () function to get the file name with Chinese characters
- To set a region method with the setlocale () function
- Preg_replace () Regular Expression method
Here's how each method implements BaseName () supports Chinese
The first method: the setlocale () function sets the area
The basename () function depends on the region, so we need to use setlocale () to set the area
<?php setlocale(LC_ALL, ‘zh_CN.GBK‘); $path = "/home/www/data/用户.txt"; $filename = basename($path); print $filename;?>
Output: User. txt
Second method: Preg_replace () Regular expression
<?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;?>
Output: User. txt
PHP basename function Problem Solving method for Chinese path under Linux