strrchr後輸出結果不同

來源:互聯網
上載者:User
$str1 = "test:http://google.com,http://hbaidu.com";$str2 = strrchr($str1,"http://");echo $str2;

輸出結果應該是:

http://hbaidu.com

但是為什麼輸出結果是:

baidu.com

如果把代碼中http://hbaidu.com的h去掉

$str1 = "test:http://google.com,http://baidu.com";$str2 = strrchr($str1,"http://");echo $str2;

結果輸出是:

http://baidu.com

請問各位大神這是為什嗎?謝謝

回複內容:

$str1 = "test:http://google.com,http://hbaidu.com";$str2 = strrchr($str1,"http://");echo $str2;

輸出結果應該是:

http://hbaidu.com

但是為什麼輸出結果是:

baidu.com

如果把代碼中http://hbaidu.com的h去掉

$str1 = "test:http://google.com,http://baidu.com";$str2 = strrchr($str1,"http://");echo $str2;

結果輸出是:

http://baidu.com

請問各位大神這是為什嗎?謝謝

建議先看看 strrchr 的文檔

http://php.net/manual/zh/function.strrchr.php

string strrchr ( string $haystack , mixed $needle )

該函數返回 haystack 字串中的一部分,這部分以 needle 的最後出現位置開始,直到 haystack 末尾。

haystack
在該字串中尋找。

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

也就是說,後面的參數只識別第一個字元, 也就是只識別你的 hh 之後的參數全部忽略。如果要使用字串,需要換用 strpos 等其他支援字串查詢的函數。

(註:樓上的說明也是不準確的,這個和 trim 也不一樣,strrchr 只識別第一個字元,其他的字元無效,而不是一個個嘗試)

  • 相關關鍵詞:
    相關文章

    聯繫我們

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