php字串函數學習之substr()_php技巧

來源:互聯網
上載者:User

<?php
/*
定義和用法
substr() 函數返回提取的子字串, 或者在失敗時返回 FALSE。

文法
substr(string,start,length)

參數 描述
string 必需。規定要返回其中一部分的字串。
start 
必需。規定在字串的何處開始。
非負數 - 從 string 的 start 位置開始,從 0 開始計算.
負數 - 從 string 結尾處向前數第 start 個字元開始。
如果 string 的長度小於或等於 start,將返回 FALSE。

length 
可選。規定要返回的字串長度。預設是直到字串的結尾。
正數 - 從 start 處開始最多包括 length 個字元(取決於 string 的長度)。
負數 - 去除從string 結尾處向前length個字元
如果提供了值為 0,FALSE 或 NULL 的 length,那麼將返回一個Null 字元串。
*/

$str = "abcdefghijklmn";

$rest = substr($str, 0);  // 返回 "abcdefghijklmn"
echo $rest . "<br/>";

$rest = substr($str, 1, 3);  // 返回 "bcd"
echo $rest . "<br/>";

$rest = substr($str, -3);  // 返回 "lmn"
echo $rest . "<br/>";

$rest = substr($str, -3, 2);  // 返回 "lm"
echo $rest . "<br/>";

$rest = substr($str, 1, -3);  // 返回 "bcdefghijk"
echo $rest . "<br/>";

$rest = substr($str, -7, -3);  // 返回 "hijk"
echo $rest . "<br/>";
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.