mb_strrpos

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_strrpos — 查找字符串在一个字符串中最后出现的位置

说明

function mb_strrpos(
    string $haystack,
    string $needle,
    int $offset = 0,
    ?string $encoding = null
): int|false

基于字符数执行一个多字节安全的 strrpos() 操作。 needle 的位置是从 haystack 的开始进行统计的。 第一个字符的位置是 0,第二个字符的位置是 1。

参数

haystack

查找 needle 在这个 string 中最后出现的位置。

needle

在 haystack 中查找这个 string。

offset
可以指定从 haystack 的任意字符位置开始搜索。负值将在 haystack 结尾前的某个点停止搜索。
encoding
encoding 参数为字符编码。如果省略或是 null,则使用内部字符编码。

返回值

返回 string 的 haystack 中,needle 最后出现位置的数值。 如果没有找到 needle,它将返回 false。

错误/异常

  • If offset is greater than the length of haystack, a ValueError will be thrown.

更新日志

版本 说明
8.0.0 现在 needle 接受空字符串。
8.0.0 已经删除将 encoding 作为第三个参数而不是 offset 传递。
8.0.0 现在 encoding 可以为 null。

参见

  • mb_strpos() - 查找字符串在另一个字符串中首次出现的位置
  • mb_internal_encoding() - 设置/获取内部字符编码
  • strrpos() - 计算指定字符串在目标字符串中最后一次出现的位置

+添加备注

用户贡献的备注 2 notes

up
2
Anonymous
20 years ago
mb_strrpos throws a warning if $haystack is empty. 
strrpos simply returns FALSE.

This is something to be wary of if overloading the mb functions.
up
1
Anonymous
11 years ago
"Negative values will stop searching at an arbitrary point prior to the end of the string. " ist misleading.
The needle may not fully part of searchrange, defined by a negative offset. 
A negative offsets marks the last byte, where a search could start.
<?php
$test = "Hallo, Herr Gött";
var_dump(strlen($test));                       // int(17)
var_dump(mb_strrpos($test,'ött',13));  // int(13)
var_dump(mb_strrpos($test,'ött',-4)); // int(13) 17-4 = 13
var_dump(mb_strrpos($test,'ött',-5)); // bool(false)
?>
To Top