查詢

substr()函式—用法及示例

「 返回擷取的字串 」


函式名:substr() 

適用版本:PHP 4, PHP 5, PHP 7

用法: substr(string $string, int $start, ?int $length = null): string

引數:

  • $string:要擷取的字串。
  • $start:擷取的起始位置,如果為負數,則從字串末尾開始計算。
  • $length(可選):擷取的長度,如果未指定,則擷取到字串末尾。

返回值: 返回擷取的字串。

示例:

$str = "Hello, World!";
echo substr($str, 0, 5); // 輸出 "Hello"

$str = "Hello, World!";
echo substr($str, 7); // 輸出 "World!"

說明: substr() 函式用於擷取字串的一部分。可以透過指定起始位置和可選的長度來擷取字串。如果未指定長度,則擷取到字串末尾。起始位置可以是正數(從字串開頭計算)或負數(從字串末尾計算)。擷取的字串作為函式的返回值返回。

補充糾錯
上一個函式: str_rot13()函式
下一個函式: SVM::crossvalidate()函式
熱門PHP函式
分享連結