这两个函数区别在于:
PHP中strpos、strstr和stripos、stristr函数分析
什么是strpos函数和strstr函数?
strpos
函数:查找字符串首次出现的位置。strstr
函数:查找字符串在另外一个字符串中的第一次出现。
这两个函数区别在于:
strpos
返回的是目标字符串在原字符串中的起始位置;strstr
返回的则是目标字符串之后,原字符串剩余部分,包括目标字符串本身。
strpos函数的语法
int strpos( string $haystack, mixed $needle, int $offset = 0 )
参数说明:
haystack
: 要被查找的主字符串;needle
: 必需,规定需要在主字符串中查找的字符串/字符;offset
: 可选,规定在主字符串中搜索的起始位置。默认是 0。
方案一:查找一个子字符串在原字符串中首次出现的位置
$string = 'Hello World';
$needle = 'World';
$position = strpos($string, $needle);
if ($position === false) {
echo '没有找到相关内容。';
} else {
echo '子字符串在原字符串第 ' . ($position + 1) . ' 个位置。';
}
以上示例中,定义了一个字符串 $string
和一个子字符串 $needle
。strpos
函数会在 $string
中查找 $needle
,如果找到则返回 $needle
在 $string
中的位置,否则返回 false。
方案二:查找一个字符串在另一个字符串中第一次出现
$mainString = 'The quick brown fox jumps over the lazy dog ';
$subString = 'brown';
$remainingString = strstr($mainString, $subString);
if ($remainingString) {
echo $subString.'在'.$mainString.'中的第一个位置之后的字符串为 '.$remainingString;
} else {
echo '未找到相关内容。';
}
以上示例中,定义了一个主字符串 $mainString
和一个子字符串 $subString
。strstr
函数会在 $mainString
中查找 $subString
,如果找到,则返回 $subString
在 $mainString
中第一次出现的位置,以及 $mainString
中 $subString
之后余下的字符串。如果没有找到,则返回 false。
什么是stripos函数和stristr函数?
stripos
函数:查找字符串中第一次出现的位置(不区分大小写)。stristr
函数:查找字符串在另外一个字符串中的第一次出现(不区分大小写)。
这两个函数与 strpos
和 strstr
差不多,但是它们不区分大小写。
stripos函数和stristr函数的语法
int stripos(string $haystack, mixed $needle, int $offset = 0)
string stristr(string $haystack, mixed $needle, bool $before_needle = false)
参数说明:
haystack
: 要被查找的主字符串;needle
: 必需,规定需要在主字符串中查找的字符串/字符;offset
: 可选,规定在主字符串中搜索的起始位置。默认是 0。before_needle
: 可选,如果设置为 TRUE,则 stristr() 函数返回 needle 之前的部分,否则返回 needle 之后的部分。默认是 FALSE。
方案一:查找一个子字符串在原字符串中首次出现的位置(不区分大小写)
$string = 'Hello World';
$needle = 'world';
$position = stripos($string, $needle);
if ($position === false) {
echo '没有找到相关内容。';
} else {
echo '子字符串在原字符串第 ' . ($position + 1) . ' 个位置。';
}
以上示例中,同样定义了一个字符串 $string
和一个子字符串 $needle
,stripos
函数区分大小写,会在 $string
中查找 $needle
,因为 $needle
的大小写和在 $string
中出现的大小写不一样,所以这里我们使用了 stripos
函数来查找字符串。函数会返回 $needle
在 $string
中的位置,否则返回 false。
方案二:查找一个字符串在另一个字符串中第一次出现(不区分大小写)
$mainString = 'The quick brown fox jumps over the lazy dog ';
$subString = 'BRown';
$remainingString = stristr($mainString, $subString);
if ($remainingString) {
echo $subString.'在'.$mainString.'中的字符串为 '.$remainingString;
} else {
echo '未找到相关内容。';
}
以上示例中,同样定义了一个主字符串 $mainString
和一个子字符串 $subString
,stristr
函数会忽略大小写在 $mainString
中查找 $subString
,如果找到,则返回 $subString
在 $mainString
中第一次出现的位置,以及 $mainString
中 $subString
之后余下的字符串。如果没有找到,则返回 false。
本文标题为:PHP中strpos、strstr和stripos、stristr函数分析
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- PHP简单实现解析xml为数组的方法 2022-10-15
- PHP基于DateTime类解决Unix时间戳与日期互转问题【针对1970年前及2038年后时间戳】 2022-10-31
- php封装pdo实例以及pdo长连接的优缺点总结 2023-08-13
- Nginx+php配置文件及原理解析 2023-05-03
- PHP获取数组中指定的一列实例 2022-10-04
- PHP Ajax跨域问题解决方案代码实例 2023-04-25
- PHP实现文件上传后台处理脚本 2023-04-02
- PHP实现重载的常用方法实例详解 2024-01-02
- 微信、支付宝等个人免签支付系统原理与实现 2023-08-17
- 基于PHP安装zip拓展,以及libzip安装的问题 2022-10-09