PHP 8.4.0 RC4 available for testing

stripos

(PHP 5, PHP 7, PHP 8)

stripos查找字符串首次出现的位置(不区分大小写)

说明

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

返回在字符串 haystackneedle 首次出现的数字位置。

strpos() 不同,stripos() 不区分大小写。

参数

haystack

在该字符串中查找。

needle

要搜索的字符串。

Prior to PHP 8.0.0, if needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged. Depending on the intended behavior, the needle should either be explicitly cast to string, or an explicit call to chr() should be performed.

offset

可选的 offset 参数,从字符此数量的开始位置进行搜索。 如果是负数,就从字符末尾此数量的字符数开始统计。

返回值

返回 needle 存在于 haystack 字符串开始的位置(独立于偏移量)。同时注意字符串位置起始于 0,而不是 1。

如果未发现 needle 将返回 false

警告

此函数可能返回布尔值 false,但也可能返回等同于 false 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

更新日志

版本 说明
8.2.0 大小写转换不在依赖于使用 setlocale() 设置的区域。只会进行 ASCII 大小写转换。非 ASCII 字节值将通过它们的字节值进行比较。
8.0.0 needle 现在接受空字符串。
8.0.0 不再支持 int 传递给 needle
7.3.0 弃用 int 传递给 needle
7.1.0 开始支持负数的 offset

示例

示例 #1 stripos() 示例

<?php
$findme
= 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// 'a' 当然不在 'xyz' 中
if ($pos1 === false) {
echo
"The string '$findme' was not found in the string '$mystring1'";
}

// 注意这里使用的是 ===。简单的 == 不能像我们期望的那样工作,
// 因为 'a' 的位置是 0(第一个字符)。
if ($pos2 !== false) {
echo
"We found '$findme' in '$mystring2' at position $pos2";
}
?>

注释

注意: 此函数可安全用于二进制对象。

参见

  • mb_stripos() - 大小写不敏感地查找字符串在另一个字符串中首次出现的位置
  • str_contains() - 确定字符串是否包含指定子串
  • str_ends_with() - 检查字符串是否以指定子串结尾
  • str_starts_with() - 检查字符串是否以指定子串开头
  • strpos() - 查找字符串首次出现的位置
  • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置
  • strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
  • stristr() - strstr 函数的忽略大小写版本
  • substr() - 返回字符串的子串
  • str_ireplace() - str_replace 的忽略大小写版本

添加备注

用户贡献的备注 5 notes

up
41
emperorshishire at gmail dot com
15 years ago
I found myself needing to find the first position of multiple needles in one haystack. So I wrote this little function:

<?php
function multineedle_stripos($haystack, $needles, $offset=0) {
foreach(
$needles as $needle) {
$found[$needle] = stripos($haystack, $needle, $offset);
}
return
$found;
}

// It works as such:
$haystack = "The quick brown fox jumps over the lazy dog.";
$needle = array("fox", "dog", ".", "duck")
var_dump(multineedle_stripos($haystack, $needle));
/* Output:
array(3) {
["fox"]=>
int(16)
["dog"]=>
int(40)
["."]=>
int(43)
["duck"]=>
bool(false)
}
*/
?>
up
8
sorrynorealemail at example dot com
6 years ago
Unlike strpos() it seems that stripos() does NOT issue a WARNING if the needle is an empty string ''.
up
5
spam at kleppinger dot com
9 years ago
Regarding the function by spam at wikicms dot org

It is very bad practice to use the same function name as an existing php function but have a different output format. Someone maintaining the code in the future is likely to be very confused by this. It will also be hard to eradicate from a codebase because the naming is identical so each use of stripos() would have to be analyzed to see how it is expecting the output format (bool or number/bool).

Calling it string_found() or something like that would make a lot more sense for long-term use.
up
3
emanuel dot karlsson at rolfsbuss dot se
6 years ago
Finding numbers in strings requires you to cast the number to string first.

strpos("123", 2) !== strpos("123", "2")
up
3
Ian Macdonald
9 years ago
Regarding the === note, it might be worth clarifying that the correct tests for a binary found/not found condition are !==false to detect found, and ===false to detect not found.
To Top