PHP 查找子字符串的方法有:1. strpos():查找首次出现位置;2. stripos():忽略大小写,查找首次出现位置;3. strrpos():查找最后出现位置;4. strripos():忽略大小写,查找最后出现位置。

如何查找 PHP 字符串中的子字符串
PHP 提供了几种查找字符串中子字符串的方法。最常用的方法包括:
1. strpos() 函数
- 作用:查找子字符串在字符串中的首次出现位置,并返回该位置的索引。
-
语法:
`php
int strpos(string $haystack, string $needle, int $offset = 0)立即学习“PHP免费学习笔记(深入)”;
-
参数:
-
$haystack:要搜索的字符串 -
$needle:要查找的子字符串 -
$offset(可选):搜索的开始位置
-
-
返回值:子字符串首次出现的位置索引,如果没有找到,则返回 -1。
2. stripos() 函数
-
作用:与
strpos()类似,但忽略大小写。 语法:
`php
int stripos(string $haystack, string $needle, int $offset = 0)-
参数:与
strpos()相同 -
返回值:与
strpos()相同
3. strrpos() 函数
- 作用:查找子字符串在字符串中的最后出现位置,并返回该位置的索引。
语法:
`php
int strrpos(string $haystack, string $needle, int $offset = 0)-
参数:与
strpos()相同 -
返回值:子字符串最后出现的位置索引,如果没有找到,则返回 -1。
4. strripos() 函数
-
作用:与
strrpos()类似,但忽略大小写。 语法:
`php
int strripos(string $haystack, string $needle, int $offset = 0)-
参数:与
strrpos()相同 -
返回值:与
strrpos()相同











