PHP basename function Problem Solving method for Chinese path under Linux

Source: Internet
Author: User

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

    1. To set a region method with the setlocale () function
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.