字串函式庫

strpos

(PHP3 , PHP4)

strpos ---  找出字串第一次出現的位置

語法 : int strpos (string haystack, string needle [, int offset])

說明 : 

傳回參數 needle在字串 haystack中第一次出現的位置,以數字表示。不像strrpos( ),此函式可以取參數 needle全部的字串,而且會使用全部的字串。

如果找不到參數 needle,則傳回 false

注意 : 會容易的被傳回值"character found at position 0" 和 "character not found"給弄錯,下列是如何去察覺其中的差異 :

// in PHP 4.0b3 and newer: 

$pos = strpos ($mystring, "b"); 

if ($pos === false) {    // note: three equal signs 

           // not found... 

// in versions older than 4.0b3: 

$pos = strpos ($mystring, "b"); 

if (is_string ($pos) && !$pos) { 

             // not found... 

}

如果參數 needle不是字串時,它會轉換成整數並且按照字元的順序值來使用。

參數 offset允許你去指定在 haystack中從那一個字元開始搜尋,傳回的位置依然是相對於 haystack的起點。

參考 : strrpos( )  strrchr( )  substr( )  stristr( )  strstr( )


上一頁 首頁 下一頁