PHP字串函數之 strstr stristr strchr strrchr

來源:互聯網
上載者:User
  • strstr -- 尋找字串的首次出現,返回字串從第一次出現的位置開始到該字串的結尾或開始。
  • stristr -- strstr 函數的忽略大小寫版本
  • strchr -- strstr 函數的別名
  • strrchr -- 尋找字串的最後一次出現,返回字串從最後一次出現的位置開始到該字串的結尾。

strstr

尋找字串的首次出現,返回字串從第一次出現的位置開始到該字串的結尾或開始。

mixed strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  

參數說明

haystack在該字串中進行尋找。 needle如果 needle 不是一個字串,那麼它將被轉換為整型並被視為字元的順序值來使用。 before_needle若為 TRUE,strstr() 將返回 needle 在 haystack 中的位置之前的部分。

傳回值

成功:返回字串 needle 之前或之後的一部分 失敗:如果沒找到 needle,將返回 FALSE。

注意

  1. 該函數區分大小寫
  2. 如果你僅僅想確定 needle 是否存在於 haystack 中,請使用速度更快、耗費記憶體更少的 strpos()函數

樣本





stristr

strstr() 函數的忽略大小寫版本

mixed stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  

該函數與 strstr() 唯一的區別就是不區分大小寫。其他可參考strstr()


strchr

strstr() 函數的別名

mixed strchr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  

該函數等同 strstr() 。其他可參考strstr()

$email  = 'name@example.com';$behind = strchr($email, 'a');echo $behind; // 列印 ame@example.com$front = strchr($email, 'a', true); // 從 PHP 5.3.0 起echo $front; // 列印 n  ?>

strrchr

尋找字串的最後一次出現,返回字串從最後一次出現的位置開始到該字串的結尾。

mixed strrchr ( string $haystack , mixed $needle )  

參數說明

haystack在該字串中進行尋找。 needle如果 needle 包含了不止一個字元,那麼僅使用第一個字元。該行為不同於 strstr()。 如果 needle 不是一個字串,那麼將被轉化為整型並被視為字元順序值。

傳回值

成功:返回字串 needle 之後的一部分 失敗:如果沒找到 needle,將返回 FALSE。

樣本


/*【 needle 為字串 】 */$email  = 'name@example.com';$behind = strrchr($email, 'am');echo $behind; // 列印 ample.com  ?>

OneAPM for PHP 能夠深入到所有 PHP 應用內部完成應用效能管理 能夠深入到所有 PHP 應用內部完成應用效能管理和監控,包括代碼層級效能問題的可見度、效能瓶頸的快速識別與追溯、真實使用者體驗監控、伺服器監控和端到端的應用效能管理。想閱讀更多技術文章,請訪問 OneAPM官方技術部落格。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.