English | 简体中文 | 繁體中文
查询

stripos()函数—用法及示例

「 在字符串中查找指定的子字符串,并返回第一次出现的位置(不区分大小写) 」


函数名称:stripos()

适用版本:PHP 4, PHP 5, PHP 7

函数描述:stripos() 函数用于在字符串中查找指定的子字符串,并返回第一次出现的位置(不区分大小写)。

语法:int stripos ( string $haystack , mixed $needle [, int $offset = 0 ] )

参数:

  • haystack:要搜索的字符串。
  • needle:要查找的子字符串。
  • offset(可选):指定开始搜索的位置,默认为 0。

返回值:如果找到子字符串,则返回第一次出现的位置,如果未找到,则返回 false。

示例:

$haystack = 'Hello world!';
$needle = 'world';

$position = stripos($haystack, $needle);
if ($position !== false) {
    echo "子字符串 '$needle' 在字符串 '$haystack' 中的位置是 $position";
} else {
    echo "未找到子字符串 '$needle' 在字符串 '$haystack' 中";
}

输出: 子字符串 'world' 在字符串 'Hello world!' 中的位置是 6

解释:在上面的示例中,我们使用 stripos() 函数来查找子字符串 'world' 在字符串 'Hello world!' 中的位置。由于 stripos() 函数不区分大小写,所以它能找到子字符串并返回其位置。在这个例子中,子字符串 'world' 在位置 6 处第一次出现。

补充纠错
上一个函数: stripslashes()函数
下一个函数: stripcslashes()函数
热门PHP函数
分享链接