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