pathinfo()对多字节字符处理的缺陷

作者:王炜
日期:2015年07月11日

pathinfo()函数在处理路径时,在php的低版本中无法处理多字节字符,这里测试的是php5.3和php5.6 的区别

<?php

// your code goes here
echo phpversion();
print_r(pathinfo("/resources/img/stock/wxb001/美景.png"));
输出:
5.6.4-2
Array
(
    [dirname] => /resources/img/stock/wxb001
    [basename] => 美景.png
    [extension] => png
    [filename] => 美景
)
但是在php5.3.3版本中
// your code goes here
echo phpversion();

print_r(pathinfo("/resources/img/stock/wxb001/美景.png"));
//输出:5.3.3
Array
(
    [dirname] => /var/www/www.shima.jp.net/resources/img/stock/wxb001
    [basename] => .png
    [extension] => png
    [filename] =>
)
// 同时,在php5.3中basename()也会过滤掉多字节字符
echo basename('/resources/img/stock/wxb001/美景.png')
// 输出:.png
那么在低版本中可以使用下面方法来实现多字节字符的处理
//your code goes here
$file = '/resources/img/stock/wxb001/美景.png';
$file_dir = dirname($file );
$file_basename = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
$file_name = substr($file_basename, 0, strrpos($file_basename, "."));
$file_extension = end(explode(".", $file_basename));
echo $file_dir; // /resources/img/stock/wxb001
echo $file_basename;  // 美景.png
echo $file_name; // 美景
echo $file_extension; // png

欢迎转载,转载请保留链接: https://www.phpzu.com/article/2015/07/11/838.html

pathinfo()对多字节字符处理的缺陷:等您坐沙发呢!

发表评论

*

code

0