运用PHP字符串查找
int stripos ( string $haystack , string $needle [, int $offset = 0 ] ) 参数说明如下:haystack:在该字符串中查找。 needle:needle 可以是一个单字符或者多字符的字符串。如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符顺序值。 offset:可选的 offset 参数允许你指定从 haystack 中的哪个字符开始查找,返回的位置数字值仍然相对于 haystack 的起始位置。 返回 needle 存在于 haystack 字符串开始的位置(独立于偏移量)。同时注意字符串位置起始于 0,而不是 1。如果未发现 needle 就将返回 false。 示例如下:
<?php $findme = 'c'; $mystring1 = 'xyz'; $mystring2 = 'ABC'; $pos1 = stripos($mystring1, $findme); $pos2 = stripos($mystring2, $findme); var_dump($pos1); var_dump($pos2); ?> 执行结果为:bool(false) int(2) strpos() strpos() 用来查找字符串首次出现的位置。 语法如下:mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) strpos() 和 strrpos()、strripos() 不一样,strpos 的偏移量不能是负数。 示例如下:
<?php $findme = 'c'; $findme1 = 'C'; $mystring = 'ABCabc'; $pos1 = strpos($mystring, $findme); $pos2 = strpos($mystring, $findme1); var_dump($pos1); var_dump($pos2); ?> 上述代码的执行结果为:int(5)int(2) strripos() strripos() 用来计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)。 语法如下:(编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |