-
substr
(php函數)
鎖定
substr()
[1]
函數返回字符串的一部分。
如果 參數start 是負數且 length 小於或等於 start,則 length 為 0。
- 中文名
- php函數
- 外文名
- substr
- 屬 性
- php函數
- 含 義
- 函數返回字符串的一部分
- 語 法
- substr(string,start,length)
substr語法
substr(string,start,length)
substr參數
substr(string,start,length)
string | 必需。規定要返回其中一部分的字符串。 |
start | 必需。規定在字符串的何處開始。 正數 - 在字符串的指定位置開始 負數 - 在從字符串結尾的指定位置開始 0 - 在字符串中的第一個字符處開始 |
length | 可選。規定要返回的字符串長度。默認是直到字符串的結尾。 正數 - 從 start 參數所在的位置向後返回字符個數 負數 - 從字符串末端指定位置向前返回字符個數 |
例子:
<?php
echo substr("Hello world!",-6);
?>
輸出:world!
echo substr("abcdef", -1); // 輸出 "f"
echo substr("abcdef", -2); // 輸出"ef"
echo substr("abcdef", -3, 1); // 輸出"d"
echo substr("abcdef", 1, -1); //輸出 "bcde"
echo substr("abcdef",-3,-1); //輸出 "de"
- 參考資料
-
- 1. PHP substr() 函數 .w3cschool.2014-02-02[引用日期2014-03-02]
- 2. PHP substr() 函數 .w3school 在線教程[引用日期2012-08-11]