
- PHP 教程
- PHP - 首頁
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混合
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc & Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 比較運算子
- PHP 控制語句
- PHP - 決策制定
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - Final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET & POST
- PHP - 檔案上傳
- PHP - Cookies
- PHP - Sessions
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 清理輸入
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 預期
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的功能
- PHP - 已刪除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
PHP 檔案系統 basename() 函式
PHP 檔案系統basename()函式允許您從完整路徑中僅提取檔名稱。它通常用於從完整路徑字串中提取檔名。
basename()的主要目標是在忽略先前目錄名稱的同時提取路徑的尾隨名稱元件。
假設您知道檔案的完整地址,例如“C:/documents/picture.jpg”。如果您只需要“picture.jpg”檔名,則可以使用basename()來實現此目的。
語法
以下是 PHP 檔案系統basename()函式的語法 -
string basename ( string $path [, string $suffix ] )
引數
下面提到了使用basename()函式所需的的引數 -
序號 | 引數及說明 |
---|---|
1 |
path(必需) 需要從中提取檔名的路徑字串。 |
2 |
suffix(可選) 此後綴將從提取的檔名中刪除。 |
返回值
它返回提取的檔名作為字串。
PHP 版本
basename()函式是 PHP 核心的一部分,在 PHP 4、PHP 5、PHP 7 和 PHP 8 中可用。
示例
在此示例中,我們將使用 PHP 檔案系統basename()函式來簡單地提取路徑中提供的檔名,並列印帶副檔名和不帶副檔名的檔名。因此,我們將使用basename()從路徑中刪除檔名元件,然後再將其提供給 $file 變數。
<?php // specify the directory path here $path = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php"; // $file is set to "index.php" $file = basename($path); // This will print "Full file name: index.php" echo "Full file name: " . $file . "<br>"; // $file is set to "index" $file = basename($path, ".php"); // This will print "File name without extension: index" echo "File name without extension: " . $file; ?>
輸出
這將產生以下結果 -
Full file name: index.php File name without extension: index
示例
現在,我們將使用basename()函式處理不同的副檔名。因此,在這裡我們可以使用此函式處理不同的副檔名。basename()函式將獲取每個程式碼中提到的路徑的檔名,但不包括其副檔名。
<?php // specify the directory path here $path1 = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php"; $path2 = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt"; $path3 = "/Applications/XAMPP/xamppfiles/htdocs/mac/logo.gif"; // Filename without extension: php echo "Filename without extension: " . basename($path1, ".php") . "<br>"; // Filename without extension: text echo "Filename without extension: " . basename($path2, ".txt") . "<br>"; // Filename without extension: gif echo "Filename without extension: " . basename($path3, ".gif") . "<br>"; ?>
輸出
這將產生以下結果 -
Filename without extension: index Filename without extension: myfile Filename without extension: logo
示例
處理 URL 最有可能指的是如何處理包含查詢引數的 URL 的特定示例。因此,解析 URL 以使用 parse_url() 函式獲取路徑,然後使用basename()提取並列印檔名。
<?php // specify the directory path here $url = "/Applications/XAMPP/xamppfiles/htdocs/mac/index.php?version=1"; // Parse the URL to get the path $parsed_url = parse_url($url, PHP_URL_PATH); // Use basename() on the path to get the file name $filename = basename($parsed_url); echo "Filename: " . $filename; ?>
輸出
這將導致以下結果 -
Filename: index.php
示例
因此,basename()函式也可以處理其中包含多個點的檔名。因此,我們只需將目錄路徑傳遞給basename()即可獲得此功能。
以下是此示例的簡單演示 -
<?php // specify the directory path here $path = "/Applications/XAMPP/xamppfiles/htdocs/mac/file.with.dots.txt"; // use basename() function to extract the filename $filename = basename($path); // Output: Filename: file.with.dots.txt echo "Filename: " . $filename; ?>
輸出
這將產生以下結果 -
Filename: file.with.dots.txt
總結
PHP basename()函式是一個非常有用的功能,用於提取給定路徑字串的檔名部分。實際上,它為使用者提供了一個選項,可以選擇在最後刪除任何指定的要提取的字尾。