| 详细内容 |
|
在编程中经常要用到查找字符串,或者判断字符串中是否存在某个字符,
其中的一个查找函数如下:
int strpos ( string $haystack, mixed $needle [, int $offset] )
第一个参数是被查找的字符串,
第二个参数是需要查找的字符串,
第三个可选参数是开始查找的位置;
函数返回值:
如果能查找到,则返回字符第一次出现的位置;
如果不存在,则返回Boolean值false;
示例:
<?php $mystring = ´abc´; $findme = ´a´; $pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected // because the position of ´a´ was the 0th (first) character. if ($pos === false) { echo "The string ´$findme´ was not found in the string ´$mystring´"; } else { echo "The string ´$findme´ was found in the string ´$mystring´"; echo " and exists at position $pos"; }
// We can search for the character, ignoring anything before the offset $newstring = ´abcdef abcdef´; $pos = strpos($newstring, ´a´, 1); // $pos = 7, not 0 ?>
注意:
判断是否存在,需要用恒等于号“===”来判断是否为false值。
|