3 回答

TA貢獻1876條經(jīng)驗 獲得超7個贊
不需要這一切。檢查pathinfo(),它為您提供路徑的所有組件。
手冊中的示例:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // filename is only since PHP 5.2.0
輸出代碼:
/www/htdocs
index.html
html
index
或者你只能獲得某些部分,如:
echo pathinfo('/www/htdocs/index.html', PATHINFO_EXTENSION); // outputs html

TA貢獻1866條經(jīng)驗 獲得超5個贊
作為替代方案pathinfo(),您可以使用
basename() - 返回路徑的文件名組件
PHP手冊中的示例
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
你必須提前知道擴展名才能刪除它。
- 3 回答
- 0 關(guān)注
- 901 瀏覽
添加回答
舉報