略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: mb_strrpos

2026-09-28

mb_strrpos

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

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

说明

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
可以用于指定 string 里从任意字符数开始进行搜索。 负数的值将导致搜索会终止于指向 string 末尾的任意点。
encoding

encoding 参数为字符编码。如果省略或是 null,则使用内部字符编码。

返回值

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

更新日志

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

参见

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

用户贡献的备注 2 notes

up
1
Anonymous
19 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
-2
Anonymous
9 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)
?>

官方地址:https://www.php.net/manual/en/function.mb-strrpos.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3