
- 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 - Cookie
- PHP - Session
- 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 - substr() 函式
PHP 的 substr() 函式用於從給定的字串中提取一部分字串,並將該提取的(子部分)字串作為新字串返回。
它接受兩個可選引數 start 和 length。以下是列出的重要要點
- 如果 start 為正數,則返回的字串將從 start 位置開始(從 0 開始)。
- 如果 start 為負數,則返回的字串將從字串末尾的指定位置開始。
- 如果字串長度小於 start 引數值,則將返回空字串。
以下是關於 length 引數的關鍵要點:
- 如果 length 為正數,則返回的字串將包含從“start”引數開始的那麼多字元。
- 如果 length 為負數,則將從字串末尾省略那麼多字元。
- 如果 length 給定且為 0,則將返回空字串。
- 如果省略 length 或為“null”,則子字串將從“start”位置開始,並繼續到字串的末尾。
注意:您不能向其傳遞單個 length 引數,它將始終將其視為提取字串的起始位置,而不是長度。
語法
以下是 PHP substr() 函式的語法:
substr(string $str, int $start, int $length): string
引數
此函式接受三個引數,如下所示:
- str - 指定輸入字串。
- start - 指定字串提取應從何處開始。
- length - 指定要提取的新字串的長度。
返回值
此函式返回原始字串的一部分字串。
示例 1
如果給定 start 引數且為 正數,則 PHP substr() 函式從給定的“start index”開始提取字串,並繼續到字串的末尾:
<?php $str = "Tutorialspoint"; echo "The given string is: $str"; $start = 2; echo "\nThe extraction will start at: $start"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start); ?>
輸出
以上程式產生以下輸出:
The given string is: Tutorialspoint The extraction will start at: 2 The extracted string is: torialspoint
示例 2
如果同時給出 start 和 length 引數且均為 正數,則此函式從給定的“start index”開始提取字串,並繼續到給定的長度:
<?php $str = "Hey!, I Love PHP!"; echo "The given string is: $str"; $start = 6; $length = 10; echo "\nThe extraction will start at: $start"; echo "\nThe length of the extracted string: $length"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start, $length); ?>
輸出
執行上述程式後,將顯示以下輸出:
The given string is: Hey!, I Love PHP! The extraction will start at: 6 The length of the extracted string: 10 The extracted string is: I Love PHP
示例 3
如果給定的長度為 0,則將返回一個 空 字串:
<?php $str = "Hello World!"; echo "The given string is: $str"; $start = 1; $length = 0; echo "\nThe extraction will start at: $start"; echo "\nThe length of the extracted string: $length"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start, $length); ?>
輸出
以下是上述程式的輸出:
The given string is: Hey, I Love PHP! The length of the extracted string: 0 The extracting will start at: 1 The extracted string is:
示例 4
如果給定的 start 引數為 負數,則字串將從字串“末尾”的指定位置開始:
<?php $str = "Hello World"; echo "The given string is: $str"; #if you pass start = -1, only "d" will be returned, because no more element to extract, #so it will return only d even you pass length as 5 $start = -5; $length = 5; echo "\nThe extraction will start at: $start"; echo "\nThe length of the extracted string: $length"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start, $length); ?>
輸出
執行上述程式後,將顯示以下輸出:
The given string is: Hello World The extraction will start at: -5 The length of the extracted string: 5 The extracted string is: World
示例 5
如果給定的長度為 負數,則將從字串末尾“省略”那麼多元素:
<?php $str = "Hello From TP!!"; echo "The given string is: $str"; $start = 0; $length = -3; echo "\nThe extraction will start at: $start"; echo "\nThe length of the extracted string: $length"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start, $length); ?>
輸出
上述程式生成以下輸出:
The extraction will start at: 0 The length of the extracted string: -3 The extracted string is: Hello From T
示例 6
如果省略了“length”引數,則字串將從指定的 start index 開始,並繼續提取到給定字串的“末尾”:
<?php $str = "Hey!, Welcome to TP"; echo "The given string is: $str"; $start = 6; echo "\nThe extraction will start at: $start"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start); ?>
輸出
這將產生以下結果:
The given string is: Hey!, Welcome to TP The extraction will start at: 6 The extracted string is: Welcome to TP
示例 7
如果字串“length”小於 start 引數值,則將返回一個 空 字串:
<?php $str = "I Love Coding!"; echo "The given string is: $str"; #an empty string will be returned because start > string length $start = 20; echo "\nThe extraction will start at: $start"; echo "\nThe extracted string is: "; #using substr() function echo substr($str, $start); ?>
輸出
以下是上述程式的輸出:
The given string is: I Love Coding! The extraction will start at: 20 The extracted string is:
php_function_reference.htm
廣告