阅读量:115
stristr 是 PHP 中的一个字符串搜索函数,它用于在字符串中查找首次出现的子字符串。这个函数在 PHP 5 及更高版本中是兼容的。
stristr 函数的语法如下:
stristr(string $haystack, string $needle): string|bool
参数:
$haystack:必需。要在其中搜索子字符串的主字符串。$needle:必需。要搜索的子字符串。
返回值:
- 如果找到子字符串,则返回子字符串在主字符串中首次出现的位置的索引。如果没有找到子字符串,则返回
false。
示例:
$haystack = "Hello, World!";
$needle = "World";
$result = stristr($haystack, $needle);
if ($result !== false) {
echo "The needle '$needle' was found at index: " . strpos($result, $needle);
} else {
echo "The needle '$needle' was not found.";
}
输出:
The needle 'World' was found at index: 7
请注意,stristr 是大小写不敏感的。如果你想进行大小写敏感的搜索,可以使用 strpos 函数。