温馨提示×

PHP的stripos函数怎么使用

小亿
88
2023-07-17 11:39:02
栏目: 编程语言

stripos函数是PHP中的一个字符串函数,用于在一个字符串中查找另一个字符串第一次出现的位置(不区分大小写)。它的使用方法如下:

stripos(string $haystack, mixed $needle, int $offset = 0): int|false

参数说明:

  • $haystack:要搜索的字符串。

  • $needle:要查找的子字符串。

  • $offset(可选):从哪个位置开始搜索,默认为0。

返回值:

  • 如果找到了子字符串,则返回子字符串第一次出现的位置(从0开始),如果没有找到,则返回false。

示例用法:

$string = "Hello, world!";
$position = stripos($string, "world");
if ($position !== false) {
echo "world 在字符串中的位置是:" . $position;
} else {
echo "未找到子字符串";
}

输出结果:

world 在字符串中的位置是:7

在上面的示例中,我们在字符串 “Hello, world!” 中查找子字符串 “world” 第一次出现的位置。由于该子字符串存在于原字符串中,所以返回的位置是7。

0