pathinfo()在php不一樣版本中對於對多字節字符處理的不一樣結果

phpinfo()函數在處理路徑時,在php的低版本中沒法處理多字節字符,這裏測試的是php5.3和php5.6 的區別php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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版本中
<?php
 
// 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

那麼在低版本中能夠使用下面方法來實現多字節字符的處理函數

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
// 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

 



相關文章
相關標籤/搜索