strrpos
(PHP 4, PHP 5, PHP 7, PHP 8)
strrpos — 计算指定字符串在目标字符串中最后一次出现的位置
说明
返回 haystack
字符串中 needle
最后一次出现的数字位置。
参数
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, theneedle
should either be explicitly cast to string, or an explicit call to chr() should be performed. offset
-
如果为 0 或正数,则从左到右搜索,跳过
haystack
的开头offset
个字节。如果为负数,搜索从右侧
offset
个字节开始,而不是从开头haystack
个字节开始。搜索从右往左进行,从所选字节中搜索第一个出现的needle
。注意:
这实际是在最后
offset
个字节之前寻找最后出现的needle
的位置。
返回值
返回 needle 在 haystack
字符串中存在的位置(与搜索顺序或者 offset 无关)。
注意: 字符串位置从 0 开始,而不是 1。
如果未找到 needle,则返回 false
。
示例
示例 #1 检查字串是否存在
很容易将“在位置 0 处找到”和“未发现字符串”这两种情况搞错。这是检测区别的办法:
<?php
$pos = strrpos($mystring, "b");
if ($pos === false) { // 注意: 三个等号
// 未发现...
}
?>
示例 #2 使用偏移位置进行查找
<?php
$foo = "0123456789a123456789b123456789c";
// 从第 0 个字节(从头)寻找“0”
var_dump(strrpos($foo, '0', 0));
// 从第 1 个字节(字节“0”之后)寻找“0”
var_dump(strrpos($foo, '0', 1));
// 从第 21 个字节(20 个字节之后)寻找“7”
var_dump(strrpos($foo, '7', 20));
// 从第 29 个字节(28 个字节之后)寻找“7”
var_dump(strrpos($foo, '7', 28));
// 从倒数第 5 个字节起从右向左寻找“7”
var_dump(strrpos($foo, '7', -5));
// 从倒数第 2 个字节起从右向左寻找“c”
var_dump(strrpos($foo, 'c', -2));
// 从倒数第 2 个字节起从右向左寻找“9c”
var_dump(strrpos($foo, '9c', -2));
?>
以上示例会输出:
int(0) bool(false) int(27) bool(false) int(17) bool(false) int(29)
参见
- strpos() - 查找字符串首次出现的位置
- stripos() - 查找字符串首次出现的位置(不区分大小写)
- strripos() - 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
- strrchr() - 查找指定字符在字符串中的最后一次出现
- substr() - 返回字符串的子串
用户贡献的备注 4 notes
The documentation for 'offset' is misleading.
It says, "offset may be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string."
This is confusing if you think of strrpos as starting at the end of the string and working backwards.
A better way to think of offset is:
- If offset is positive, then strrpos only operates on the part of the string from offset to the end. This will usually have the same results as not specifying an offset, unless the only occurences of needle are before offset (in which case specifying the offset won't find the needle).
- If offset is negative, then strrpos only operates on that many characters at the end of the string. If the needle is farther away from the end of the string, it won't be found.
If, for example, you want to find the last space in a string before the 50th character, you'll need to do something like this:
strrpos($text, " ", -(strlen($text) - 50));
If instead you used strrpos($text, " ", 50), then you would find the last space between the 50th character and the end of the string, which may not have been what you were intending.
The description of offset is wrong. Here’s how it works, with supporting examples.
Offset effects both the starting point and stopping point of the search. The direction is always right to left. (The description wrongly says PHP searches left to right when offset is positive.)
Here’s how it works:
When offset is positive, PHP searches right to left from the end of haystack to offset. This ignores the left side of haystack.
When offset is negative, PHP searches right to left, starting offset bytes from the end, to the start of haystack. This ignores the right side of haystack.
Example 1:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', 5));
Result: int(10)
Example 2:
$foo = "aaaaaa67890";
var_dump(strrpos($foo, 'a', 5));
Result: int(5)
Conclusion: When offset is positive, PHP searches right to left from the end of haystack.
Example 3:
$foo = "aaaaa567890";
var_dump(strrpos($foo, 'a', 5));
Result: bool(false)
Conclusion: When offset is positive, PHP stops searching at offset.
Example 4:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', -5));
Result: int(6)
Conclusion: When offset is negative, PHP searches right to left, starting offset bytes from the end.
Example 5:
$foo = "a234567890";
var_dump(strrpos($foo, 'a', -5));
Result: int(0)
Conclusion: When offset is negative, PHP searches right to left, all the way to the start of haystack.
Here is a simple function to find the position of the next occurrence of needle in haystack, but searching backwards (lastIndexOf type function):
//search backwards for needle in haystack, and return its position
function rstrpos ($haystack, $needle, $offset){
$size = strlen ($haystack);
$pos = strpos (strrev($haystack), $needle, $size - $offset);
if ($pos === false)
return false;
return $size - $pos;
}
Note: supports full strings as needle
Ten years on, Brian's note is still a good overview of how offsets work, but a shorter and simpler summary is:
strrpos($x, $y, 50); // 1: this tells strrpos() when to STOP, counting from the START of $x
strrpos($x, $y, -50); // 2: this tells strrpos() when to START, counting from the END of $x
Or to put it another way, a positive number lets you search the rightmost section of the string, while a negative number lets you search the leftmost section of the string.
Both these variations are useful, but picking the wrong one can cause some highly confusing results!