php中用於尋找字串的常用函數

來源:互聯網
上載者:User
  1. $email = 'test@jbxue.com';
  2. $domain = strstr($email, '@');
  3. echo $domain; // 列印 @example.com
  4. $user = strstr($email, '@', true); // 從 PHP 5.3.0 起
  5. echo $user; // 列印 yuxiaoxiao
  6. ?>
複製代碼

二、stristr strstr不區分大小寫版本

三、strpos -尋找字串首次出現的位置

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 注1:可選的 offset 參數可以用來指定從 haystack 中的哪一個字元開始尋找。返回的數字位置是相對於 haystack 的起始位置而言的。

四、substr -返回字串的子串

  1. string substr ( string $string , int $start [, int $length ] )
  2. $rest = substr("abcdef", -1); // 返回 "f"
複製代碼

注1:如果 start 是非負數,返回的字串將從 string 的 start 位置開始,從 0 開始計算。例如,在字串 “abcdef” 中,在位置 0 的字元是 “a”,位置 2 的字串是 “c” 等等。

注2:如果 start 是負數,返回的字串將從 string 結尾處向前數第 start 個字元開始。

注3:如果 string 的長度小於或等於 start,將返回 FALSE。

length

注4:如果提供了正數的 length,返回的字串將從 start 處開始最多包括 length 個字元(取決於 string 的長度)。

注5:如果提供了負數的 length,那麼 string 末尾處的許多字元將會被漏掉(若 start 是負數則從字串尾部算起)。如果 start 不在這段文本中,那麼將返回一個Null 字元串。

注6:如果提供了值為 0,FALSE 或 NULL 的 length,那麼將返回一個Null 字元串。

注7:如果沒有提供 length,返回的子字串將從 start 位置開始直到字串結尾。

  1. $rest = substr("abcdef", 0, -1); // 返回 "abcde"
  2. $rest = substr("abcdef", 2, -1); // 返回 "cde"
  3. $rest = substr("abcdef", 4, -4); // 返回 ""
  4. $rest = substr("abcdef", -3, -1); // 返回 "de"
  5. ?>
複製代碼

五、strrchr -尋找指定字元在字串中的最後一次出現 string strrchr ( string $haystack , mixed $needle ) 該函數返回 haystack 字串中的一部分,這部分以 needle 的最後出現位置開始,直到 haystack 末尾。

六、strripos -計算指定字串在目標字串中最後一次出現的位置(不區分大小寫)

七、stripos -尋找字串首次出現的位置(不區分大小定)

八、strrpos -計算指定字串在目標字串中最後一次出現的位置

  • 聯繫我們

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