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( )