运用PHP字符串查找

语法如下:

int stripos ( string $haystack , string $needle [, int $offset = 0 ] )

参数说明如下:

  • haystack:在该字符串中查找。
  • needle:needle 可以是一个单字符或者多字符的字符串。如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符顺序值。
  • offset:可选的 offset 参数允许你指定从 haystack 中的哪个字符开始查找,返回的位置数字值仍然相对于 haystack 的起始位置。



返回 needle 存在于 haystack 字符串开始的位置(独立于偏移量)。同时注意字符串位置起始于 0,而不是 1。如果未发现 needle 就将返回 false。



示例如下:


  1. <?php
  2. $findme = 'c';
  3. $mystring1 = 'xyz';
  4. $mystring2 = 'ABC';
  5. $pos1 = stripos($mystring1, $findme);
  6. $pos2 = stripos($mystring2, $findme);
  7. var_dump($pos1);
  8. var_dump($pos2);
  9. ?>

执行结果为:

bool(false) int(2)

strpos()

strpos() 用来查找字符串首次出现的位置。 语法如下:

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

strpos() 和 strrpos()、strripos() 不一样,strpos 的偏移量不能是负数。 示例如下:


  1. <?php
  2. $findme = 'c';
  3. $findme1 = 'C';
  4. $mystring = 'ABCabc';
  5. $pos1 = strpos($mystring, $findme);
  6. $pos2 = strpos($mystring, $findme1);
  7. var_dump($pos1);
  8. var_dump($pos2);
  9. ?>

上述代码的执行结果为:

int(5)int(2)

strripos()

strripos() 用来计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)。 语法如下:

【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章