溫馨提示×

PHP的stripos函數(shù)怎么使用

小億
91
2023-07-17 11:39:02
欄目: 編程語言

stripos函數(shù)是PHP中的一個字符串函數(shù),用于在一個字符串中查找另一個字符串第一次出現(xiàn)的位置(不區(qū)分大小寫)。它的使用方法如下:

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

參數(shù)說明:

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

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

  • $offset(可選):從哪個位置開始搜索,默認(rèn)為0。

返回值:

  • 如果找到了子字符串,則返回子字符串第一次出現(xiàn)的位置(從0開始),如果沒有找到,則返回false。

示例用法:

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

輸出結(jié)果:

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

在上面的示例中,我們在字符串 “Hello, world!” 中查找子字符串 “world” 第一次出現(xiàn)的位置。由于該子字符串存在于原字符串中,所以返回的位置是7。

0